mysql - PHP: how to save result of recursive function in array? -
i have changed version of recursive function... http://www.phpbuilder.com/articles/databases/mysql/handling-hierarchical-data-in-mysql-and-php.html
what need way save returned value of function in array can reverse order of array elements. function works me, need way save values. here code...
function display_children($category_id, $level) { global $database; $result = mysql_query("select * parents id_roditelja='$category_id'") or die(mysql_error()); $niz = array(); while ($row = mysql_fetch_array($result)) { echo str_repeat(' ', $level) . $row['naziv'] . "<br/>"; array_push($niz, display_children($row['parent_id'], $level + 1)); //this 1 way tried, , $niz exact number of elements each null //in $niz array need store values of recursion var_dump($niz); } }
i think want this:
function display_children/* <- must same */($category_id, $level) { global $database; $result = mysql_query("select * parents id_roditelja='$category_id'") or die(mysql_error()); $niz = array(); while ($row = mysql_fetch_array($result)) { $niz[] = str_repeat(' ', $level) . $row['naziv'] . "<br/>"; // need save $niz = array_merge($niz, /* -> */display_children($row['parent_id'], $level + 1)); // array_merge doesn't take reference need store result in $niz } return $niz; }
if want tree structure use array_push instead of array_merge. noone seems notice: function can't return without return statement !
Comments
Post a Comment