0

私はこれを尋ねるのは本当にばかげていると感じています. これを行う方法を本当に理解したいです。

基本的に私は出力がこのようなものになりたい

私が抱えている問題は、型が子型ではないことを確認し、サブタイプに必要な場合は無限にループする方法がわからないことです...または少なくとも3回、場合によっては4回。

ここに SQL ダンプがあります。
これが私の動作しないコードです。
これが出力しているものです。
これが私が出力したいものです(サブタイプのタイプIDが再び1から開始する代わりに使用されていることに注意してください。また、ソネットにサブタイプがある場合、ソネットに対して同じことを行うことに注意してください)。

誰かが私を助けてくれたら、それは素晴らしいことです。これを正しく行う方法を学び、理解したいのですが、とても迷っています!

最初のリンクで事前タグを使用していても、「あなたの投稿には不適切な形式のコードがあります」というリンクが表示され続けて申し訳ありません....これを投稿するのは大雑把でした:(。

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 に答える