0

HTML選択ボックスにツリーを作成するコードがあります。配列を埋めて、次のように選択ボックスにツリーを生成しました。

$vertices = array(
    array(
        'id' => 5,
        'name' => 'Child3',
        'parent' => 2,
    ),
    array(
        'id' => 3,
        'name' => 'Child1',
        'parent' => 1,
    ),
    array(
        'id' => 4,
        'name' => 'Child2',
        'parent' => 1,
    ),
    array(
        'id' => 1,
        'name' => 'Cat1',
        'parent' => null,
    ),
    array(
        'id' => 2,
        'name' => 'Cat2',
        'parent' => null,
    ),
);

/**
 * Generate tree
 */
$subtrees = $trees = array();
foreach ($vertices as $vertex) {
    $v = array(
        'id' => $vertex['id'],
        'name' => $vertex['name'],
        'children' => array(),
    );

    if (isset($subtrees[$vertex['id']])) {
        $v['children'] = $subtrees[$vertex['id']];
    }

    if ($vertex['parent'] === null) {
        $trees[] = $v;
    }
    else if (!isset($subtrees[$vertex['parent']])) {
        $subtrees[$vertex['parent']] = array($v);
    }
    else {
        $subtrees[$vertex['parent']][] = $v;
    }
}
unset($subtrees);

/**
 * Generate HTML
 */

function getSubtreeOptions(array $subtreeRoot, $level = 0)
{
    $html = sprintf('%s<option value="%d">%s%s</option>' . PHP_EOL,
        str_repeat("\t", $level + 1),
        $subtreeRoot['id'],
        $level > 0 ? str_repeat(' ', $level) . '--' : null,
        $subtreeRoot['name']);

    foreach ($subtreeRoot['children'] as $child) {
        $html .= getSubtreeOptions($child, $level + 1);
    }

    return $html;
}

echo '<select name="stuff">' . PHP_EOL;
foreach ($trees as $root) {
    echo getSubtreeOptions($root);
}
echo '</select>';

正常に動作しますが、mysqlクエリで配列を埋めたい場合、selectboxに何も表示されません

これは、配列を埋めるための私のdbコードです:

$res = mysql_query('SELECT id, name, parent FROM groups ORDER BY parent DESC, id') or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);
$vertices = array();
while ($vertex = mysql_fetch_assoc($res)) {
    $vertices[] = $vertex;
}

問題はどこだ?

配列を印刷すると、出力は次のようになります。

Array
(
    [0] => Array
        (
            [id] => 4
            [name] => test
            [parent] => 3
        )

    [1] => Array
        (
            [id] => 2
            [name] => test
            [parent] => 1
        )

    [2] => Array
        (
            [id] => 3
            [name] => test
            [parent] => 1
        )

    [3] => Array
        (
            [id] => 1
            [name] => test
            [parent] => 0
        )

    [4] => Array
        (
            [id] => 5
            [name] => test
            [parent] => 0
        )

)
4

1 に答える 1

0

子を持たないノードのキー入力を置き換えれ0ば問題ありません。あなたの配列は次のようになりますnullparent$verties$vertices

Array
(
[0] => Array
    (
        [id] => 4
        [name] => test
        [parent] => 3
    )

[1] => Array
    (
        [id] => 2
        [name] => test
        [parent] => 1
    )

[2] => Array
    (
        [id] => 3
        [name] => test
        [parent] => 1
    )

[3] => Array
    (
        [id] => 1
        [name] => test
        [parent] => null
    )

[4] => Array
    (
        [id] => 5
        [name] => test
        [parent] => null
    )

 )

状態にある理由

 if ($vertex['parent'] === null) {
    $trees[] = $v;
}

あなたは反対ですnull

于 2013-05-15T06:57:26.847 に答える