1

您好,我有一个未定义的偏移量 0 。在这段代码里面。

$q = mysql_query("SELECT * FROM category");
if (false === $q) {
    echo mysql_error();
}

while ($r = mysql_fetch_row($q)) {
  $names[$r[0]] = $r[2];
  $children[$r[0]][] = $r[1];
}

function render_select($root=0, $level=-1) {
  global $names, $children;
  if ($root != 0)
    echo '<option>' . strrep(' ', $level) . $names[$root] . '</option>';
  foreach ($children[$root] as $child)
    render_select($child, $level+1);
}

echo '<select>';
render_select();
echo '</select>';

错误所在的确切行:

foreach ($children[$root] as $child)
    render_select($child, $level+1);

这是一个树格式的选择框,我在这个问题中找到了这个代码

更高效的层级系统

4

1 回答 1

1

There is some ambiguity in your code here:

if ($root != 0)
    echo '<option>' . strrep(' ', $level) . $names[$root] . '</option>';
  foreach ($children[$root] as $child)
    render_select($child, $level+1);

If you are attempting to execute these three lines only if $root != 0, you will need to add curly braces like this:

if ($root != 0)
{
    echo '<option>' . strrep(' ', $level) . $names[$root] . '</option>';
    foreach ($children[$root] as $child)
    {
        render_select($child, $level+1);
    }
}

Otherwise, anytime render_select is called without a parameter (or with a first parameter value of '0') you will attempt to access the element of $children at array key '0'. As your error indicates, $children does not contain a value at that key.

于 2013-05-09T20:09:04.047 回答