I have changed version of this recursive function... http://www.phpbuilder.com/articles/databases/mysql/handling-hierarchical-data-in-mysql-and-php.html
What I need is a way to save the returned value of this function in an array so that I can reverse the order of array elements. The function works well for me, but I need a way to save values. Here is the code...
function display_children($category_id, $level) {
global $database;
$result = mysql_query("SELECT * FROM parents WHERE 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 is one way I tried, and I get $niz with exact number of elements but each is null
//in this $niz array I need to store values of recursion
var_dump($niz);
}
}
w5m
2,3463 gold badges35 silver badges47 bronze badges
-
chould you dump $niz outside the while, your null problem is probably because of the second parameter given to array_push. Your recursive function does not return any value to be send to array_push, I mean you have lots of operation, but no value at the endIvan Yonkov– Ivan Yonkov2013年08月28日 08:54:16 +00:00Commented Aug 28, 2013 at 8:54
-
@som: can you explain why this is not a recursive function ? It calls itself ? Thug: you forgot return $niz; after your whileChristoph Diegelmann– Christoph Diegelmann2013年08月28日 09:00:10 +00:00Commented Aug 28, 2013 at 9:00
-
Sorry I don't see properly.som– som2013年08月28日 09:04:40 +00:00Commented Aug 28, 2013 at 9:04
1 Answer 1
I think you want this:
function display_children/* <- this must be the same */($category_id, $level) {
global $database;
$result = mysql_query("SELECT * FROM parents WHERE id_roditelja='$category_id'") or die(mysql_error());
$niz = array();
while ($row = mysql_fetch_array($result)) {
$niz[] = str_repeat(' ', $level) . $row['naziv'] . "<br/>"; // we need to save this
$niz = array_merge($niz, /* as this -> */display_children($row['parent_id'], $level + 1)); // array_merge doesn't take a reference so we need to store its result in $niz
}
return $niz;
}
if you want a tree like structure use array_push instead of array_merge. As noone seems to notice: His function can't return anything without a return statement !
answered Aug 28, 2013 at 9:02
-
As maybe we have already noticed that, but in comments, because one more row, does not deserve a whole answer :)Ivan Yonkov– Ivan Yonkov2013年08月28日 09:08:50 +00:00Commented Aug 28, 2013 at 9:08
-
Same result as my way...null again. My function returns names of products, as it should, but i need it saved in array, because order of elements in not the right.Thug– Thug2013年08月28日 09:19:44 +00:00Commented Aug 28, 2013 at 9:19
-
@Thug your function (how you posted it) may display something but it won't return anything because it lacks a return statement ! Can you post how you call your function and an example output ?Christoph Diegelmann– Christoph Diegelmann2013年08月28日 09:25:50 +00:00Commented Aug 28, 2013 at 9:25
-
This is example od output Zaštita za zube Borilački aksesoari Borilački sportovi Sportska oprema array 0 => null array 0 => null array 0 => null array 0 => nullThug– Thug2013年08月28日 09:43:33 +00:00Commented Aug 28, 2013 at 9:43
-
i call function like display_chield(58,4);Thug– Thug2013年08月28日 09:46:42 +00:00Commented Aug 28, 2013 at 9:46
default