1

問題:

MySQLの関数とデータを使用して再帰ツリーを構築しようとしています。ただし、結果は期待どおりではありません。

PHPコード:

function buildTree($root, $next = array()) 
{
    // Sanitize input
    $root = (int) $root;

    // Do query
    $query = "SELECT CID, Item, Parent FROM betyg_category WHERE Status = '1' AND Parent = '{$root}'";
    $result = mysql_query($query) or die ('Database Error (' . mysql_errno() . ') ' . mysql_error());

    // Loop results
    while ($row = mysql_fetch_assoc($result)) 
    {
      $next[$row['CID']] = array (
                                'CID' => $row['CID'], 
                                'Item' => $row['Item'], 
                                'Parent' => $row['Parent'], 
                                'Children' => buildTree($row['CID'], $next)
                            );
    }

    // Free mysql result resource
    mysql_free_result($result);

    // Return new array
    return $next;
}

$testTree = buildTree(0);

echo "<xmp>".print_r($testTree, true)."</xmp>";

データベース内のテーブルは次のようになります。

ここに画像の説明を入力してください

配列を次のようにしたいと思います。

Array
(
    [1] => Array
    (
        [CID] => 1
        [Item] => Litteratur
        [Parent] => 0
        [Children] => Array
            (
                [2] => Integration av källorna
                [3] => Belysning av egna resultat
                [4] => Referenser
            )

    )

    and so forth..
)

つまり、親ごとに=>子を作成してから、次の親に移動するなどです。アドバイスをよろしくお願いします。

4

3 に答える 3

2

ここでは再帰は必要ありません。実際、SELECT N+1 の問題が発生するため、非常に非効率的です。親によって結果セットを並べ替えるだけです。

$query = "SELECT CID, Item, Parent FROM betyg_category WHERE Status = '1' ORDER BY Parent";
$result = mysql_query($query);

$tree = array();
while($row = mysql_fetch_assoc($result)) {
    if($row['Parent'] == 0) {
        $row['Children'] = array();
        $tree[$row['CID']] = $row;
    } else {
        $tree[$row['Parent']]['Children'][] = $row;
    }
}

これにより、以下が生成されます。

Array
(
    [1] => Array
        (
            [CID] => 1
            [Item] => Litteratur
            [Parent] => 0
            [Children] => Array
                (
                    [0] => Array
                        (
                            [CID] => 2
                            [Item] => Integration av källorna
                            [Parent] => 1
                        )

                    [1] => Array
                        (
                            [CID] => 3
                            [Item] => Belysning
                            [Parent] => 1
                        )

                    [2] => Array
                        (
                            [CID] => 4
                            [Item] => Referenser
                            [Parent] => 1
                        )

                )

        )

    [5] => Array
        (
            [CID] => 5
            [Item] => Validitet
            [Parent] => 0
            [Children] => Array
                (
                    [0] => Array
                        (
                            [CID] => 6
                            [Item] => Huvudsyfte
                            [Parent] => 5
                        )

                )

        )

)

各子の名前のみが必要な場合は、変更して使用し$tree[$row['Parent']]['Children'][] = $row['Item']ます。代わりは。

于 2012-06-08T15:53:58.197 に答える
2

これを試してください:

$array = array();
while ($row = mysql_fetch_assoc($result)) 
{
    if($row['parent'] == '0')
    {
        $array[$row['parent']] = '';
        $array[$row['parent']]['CID'] = $row['CID'];
        $array[$row['parent']]['Item'] = $row['item'];
        $array[$row['parent']]['Parent'] = $row['parent'];
        $array[$row['parent']]['Children'] = '';

    }
    else
    {
        $array[$row['parent']]['Children'][$row['CID']] = $row['item'];
    }
}
echo "<pre>";
print_r($array);

クエリの最初。追加ORDER BY CID ASC してから

$count = array_keys($array);
foreach($count as $arr)
{
    ksort($array[$arr]['Children']);
}
于 2012-06-08T16:05:26.320 に答える
0

すべてのコメントからの最終的な解決策:

$query = "SELECT * FROM betyg_category WHERE Status = '1' ORDER BY CID ASC";
$result = mysql_query($query) or die ('Database Error (' . mysql_errno() . ') ' . mysql_error());

$tree = array();

while($row = mysql_fetch_assoc($result)) 
{
    if($row['Parent'] == 0) 
    {
        $row['Children'] = array();
        $tree[$row['CID']] = array(
                                'CID' => $row['CID'], 
                                'Item' => $row['Item'], 
                                'Parent' => $row['Parent']
                            );
    } 
    else 
    {
        $tree[$row['Parent']]['Children'][$row['CID']] = $row['Item'];
    }
}

$count = array_keys($tree);

foreach ($count as $array)
{
    ksort($tree[$array]['Children']);
}

echo "<xmp>".print_r($tree, true)."</xmp>";
于 2012-06-08T16:27:59.337 に答える