简体   繁体   English

创建遍历数据库信息的多维数组

[英]Creating multidimentional arrays that loop through database info

I feel really stupid asking this and I've searched around before and had a bit of help but not really. 我问这个问题真的很愚蠢,我之前已经四处搜寻,并获得了一些帮助,但实际上不是。 I really want to understand how to do this. 我真的很想了解如何做到这一点。

Basically I want the output to be something like this 基本上我希望输出是这样的

The problem I am having is I am not sure how to make sure the type is not a child type and to loop through infinitely if needed for subtypes... or at least 3, possibly 4 times. 我遇到的问题是我不确定如何确定类型不是子类型,如果需要,则无限循环遍历子类型……或至少3次,可能是4次。

Here's an SQL dump . 这是一个SQL转储
Here's my not working code . 这是我不工作的代码
Here's what it's outputting . 是它的输出
Here's what I want it to output (note the type IDs in the subtypes is being used instead of starting from 1 again, note also that if sonnet had a sub type it would do the same thing for sonnet). 就是我要输出的内容 (请注意,正在使用子类型中的类型ID,而不是再次从1开始,还请注意,如果十四行诗具有子类型,它将对十四行诗做同样的事情)。

If anyone could help me at all that would be fantastic. 如果有人可以帮助我,那将是很棒的。 I really want to learn and understand how to do this correctly but I'm very lost! 我真的很想学习并理解如何正确执行此操作,但是我很迷茫!

Sorry for all the links I keep getting "your post has improperly formatted code" even using pre tags on that first link....it's been rough posting this :(. 很抱歉,即使我在第一个链接上使用前置标签,我仍然收到“您的帖子的代码格式不正确”的所有链接。...发布这个:(。

With such a big code sample, it's hard to do any kind of testing, but this might get you a bit closer to your goal: 有了这么大的代码示例,很难进行任何类型的测试,但这可能使您更接近目标:

<?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>'); 
                    }
            }
    } ?>

I finally got it to work with this :) 我终于可以使用它了:)

 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; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM