php - Undefined Offset 0 , how can i set this array -
hello have undefined offset 0 . inside code.
$q = mysql_query("select * 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] $child) render_select($child, $level+1); } echo '<select>'; render_select(); echo '</select>';
the exact line error :
foreach ($children[$root] $child) render_select($child, $level+1);
this selectbox tree format, found code in question
there ambiguity in code here:
if ($root != 0) echo '<option>' . strrep(' ', $level) . $names[$root] . '</option>'; foreach ($children[$root] $child) render_select($child, $level+1);
if attempting execute these 3 lines if $root != 0
, need add curly braces this:
if ($root != 0) { echo '<option>' . strrep(' ', $level) . $names[$root] . '</option>'; foreach ($children[$root] $child) { render_select($child, $level+1); } }
otherwise, anytime render_select
called without parameter (or first parameter value of '0') attempt access element of $children @ array key '0'. error indicates, $children not contain value @ key.
Comments
Post a Comment