0

问这个问题我觉得真的很愚蠢,我之前已经四处搜寻过,并得到了一些帮助,但不是真的。我真的很想了解如何做到这一点。

基本上我希望输出是这样的

我遇到的问题是我不确定如何确保该类型不是子类型并在子类型需要时无限循环……或者至少 3 次,可能 4 次。

这是一个 SQL 转储
这是我不工作的代码
是它输出的内容。
是我希望它输出的内容(请注意正在使用子类型中的类型 ID,而不是再次从 1 开始,另请注意,如果十四行诗有子类型,它会对十四行诗做同样的事情)。

如果有人可以帮助我,那就太好了。我真的很想学习和理解如何正确地做到这一点,但我很迷茫!

抱歉,即使在第一个链接上使用 pre 标记,我也不断收到“您的帖子的代码格式不正确”的所有链接....发布此内容很粗糙:(。

4

2 回答 2

0

有了这么大的代码示例,很难进行任何类型的测试,但这可能会让您更接近您的目标:

<?php
function getCategoriesTypes($workIndexOptions)
    {
        global $smcFunc, $scripturl, $user_info, $modSettings, $txt;
        global $settings, $options, $context, $sourcedir;

        $result_work_cats_and_types = $smcFunc['db_query']('workindex_fetch_cats_and_types' , '
                SELECT
                        c.id_cat,
                        c.cat_order,
                        c.cat_name,
                        c.cat_desc,
                        t.id_type,
                        t.id_cat AS parent_cat,
                        t.id_parent,
                        t.child_level,
                        t.type_order,
                        t.type_name,
                        t.type_desc,
                        t.num_works,
                        t.num_comments,
                        t.unapproved_comments
                FROM
                        {db_prefix}works_categories AS c,
                        {db_prefix}works_types AS t
                WHERE
                        c.id_cat = t.id_cat

        ');

        // Start with an empty array.
        $work_categories = array();

        while ($row = $smcFunc['db_fetch_assoc']($result_work_cats_and_types)) {
                if (!isset($work_categories[$row['id_cat']])) {
                        $work_categories[$row['id_cat']] = array(
                                'id' => $row['id_cat'],
                                'order' => $row['cat_order'],
                                'name' => $row['cat_name'],
                                'description' => $row['cat_desc'],
                                'href' => $scripturl . '#c' . $row['id_cat'],
                                'types' => array()
                            );
                    }

                $idCat      =   $row['id_cat'];
                $idType     =   $row['id_type'];
                $idOrder    =   $row['order'];
                // You shouldn't generate these two arrays at the same time on every loop
                // Try doing if for the first, else for the rest.
                // Identify a unique element like order and child.
                // Assuming `child_level` will always be 0 for parent and signify new
                // array when `order` changes (I am not sure, it's hard to tell how you
                // are signifying organizing triggers. The order seems common to child and parent arrays...??)
                if($row['child_level'] == 0 && !isset($work_categories[$idCat]['types'][$idOrder])) {
                        // I put $row['order'] here as the identifying array key because I don't 
                        // see any other difference from your other keys that indicate new array
                        $work_categories[$idCat]['types'][$idOrder] = array(
                                'id' => $row['id_type'],
                                'order' => $row['type_order'],
                                'parent_cat' => $row['parent_cat'],
                                'child_level' => $row['child_level'],
                                'name' => $row['type_name'],
                                'description' => $row['type_desc'],
                                'works' => $row['num_works'],
                                'comments' => $row['num_comments'],
                                'href' => $scripturl . '?type=' . $row['id_type'] . '.0',
                                'link' => '<a href="' . $scripturl . '?type=' . $row['id_type'] . '.0">' . $row['type_name'] . '</a>',
                                'types' => array());
                    }
                else {
                        // Use the `order` key here (I am assuming)
                        $work_categories[$idCat]['types'][$idOrder]['types'][$idType] = array(
                            'id' => $row['id_type'],
                            'order' => $row['type_order'],
                            'parent_cat' => $row['parent_cat'],
                            'child_level' => $row['child_level'],
                            'name' => $row['type_name'],
                            'description' => $row['type_desc'],
                            'works' => $row['num_works'],
                            'comments' => $row['num_comments'],
                            'href' => $scripturl . '?type=' . $row['id_type'] . '.0',
                            'link' => '<a href="' . $scripturl . '?type=' . $row['id_type'] . '.0">' . $row['type_name'] . '</a>'); 
                    }
            }
    } ?>
于 2014-12-18T17:00:45.430 回答
0

我终于让它和这个一起工作了:)

function getCategoriesTypes($workIndexOptions)
{
	global $smcFunc, $scripturl, $user_info, $modSettings, $txt;
	global $settings, $options, $context, $sourcedir;

		$result_work_cats_and_types = $smcFunc['db_query']('workindex_fetch_cats_and_types' , '
		SELECT
			c.id_cat,
			c.cat_order,
			c.cat_name,
			c.cat_desc,
			t.id_type,
			t.id_cat AS parent_cat,
			t.id_parent,
			t.child_level,
			t.type_order,
			t.type_name,
			t.type_desc,
			t.num_works,
			t.num_comments,
			t.unapproved_comments
		FROM
			{db_prefix}works_categories AS c,
			{db_prefix}works_types AS t
		WHERE
			c.id_cat = t.id_cat
		ORDER BY
			c.cat_order, t.id_parent
	');

	// Start with an empty array.
	$work_categories = array();
	
	while ($row = $smcFunc['db_fetch_assoc']($result_work_cats_and_types))
	{
		if (!isset($work_categories[$row['id_cat']]))
		{
			$work_categories[$row['id_cat']] = array(
				'id' => $row['id_cat'],
				'order' => $row['cat_order'],
				'name' => $row['cat_name'],
				'description' => $row['cat_desc'],
				'href' => $scripturl . '#c' . $row['id_cat'],
				'types' => array() ,
			);
		}

		
		if (($work_categories[$row['child_level']]) == 0)
		{
		$work_categories[$row['id_cat']]['types'][$row['id_type']] = array(
			'id' => $row['id_type'],
			'order' => $row['type_order'],
			'parent_cat' => $row['parent_cat'],
			'parent' => $row['id_parent'],
			'child_level' => $row['child_level'],
			'name' => $row['type_name'],
			'description' => $row['type_desc'],
			'works' => $row['num_works'],
			'comments' => $row['num_comments'],
			'href' => $scripturl . '?type=' . $row['id_type'] . '.0',
			'link' => '<a href="' . $scripturl . '?type=' . $row['id_type'] . '.0">' . $row['type_name'] . '</a>',
			'types' => array() ,
		);
		}
	
		if (($work_categories[$row['child_level']]) > 0)
		{
		$work_categories[$row['id_cat']]['types'][$row['id_parent']]['types'][$row['id_type']] = array(
			'id' => $row['id_type'],
			'order' => $row['type_order'],
			'parent_cat' => $row['parent_cat'],
			'parent' => $row['id_parent'],
			'child_level' => $row['child_level'],
			'name' => $row['type_name'],
			'description' => $row['type_desc'],
			'works' => $row['num_works'],
			'comments' => $row['num_comments'],
			'href' => $scripturl . '?type=' . $row['id_type'] . '.0',
			'link' => '<a href="' . $scripturl . '?type=' . $row['id_type'] . '.0">' . $row['type_name'] . '</a>',
		);
		}
	}
	$smcFunc['db_free_result']($result_work_cats_and_types);
	
	// Let's return something....
	return $work_categories;


}

于 2014-12-22T06:29:33.347 回答