5

I couldn't find solution for this and I don't have much time for this. So what I want is to make function that I give category ID and it returns all ID's of categories which are it's child categories.

function getID($var) {
 $categories = array();
 function getChildren($id) {
 $result = mysql_query("SELECT * FROM categories WHERE parentID = '$id'");
 echo "<ul>";
 while ($row = mysql_fetch_array($result)) {
 echo "<li><a>{$row['ID']}</a>";
 $categories[] = $row['ID'];
 getChildren($row['ID']);
 echo "</li>";
 }
 echo "</ul>";
 }
 getChildren($var);
 return $categories;
}

I wan't to store everything in $categories array. $var is category ID which I give to function. When I call this function it prints list of exactly what I want't but array is empty.

asked Mar 15, 2013 at 8:03
5
  • it prints list of exactly what I want't but array is empty it prints and its empty!?!? Commented Mar 15, 2013 at 8:05
  • echo "<li><a>{$row['ID']}</a>" this line outputs ID what I want but when I try to print_r($categories) it doesn't output anything. Commented Mar 15, 2013 at 8:07
  • Do you want to return a flat list or a tree that represents the children? Commented Mar 15, 2013 at 8:08
  • I want just flat list. Commented Mar 15, 2013 at 8:16
  • Then fredriks answer should do the trick Commented Mar 15, 2013 at 8:17

3 Answers 3

12

It seems you have a scope problem. Try this:

function getChildren(&$categories, $id) {
 $result = mysql_query("SELECT * FROM categories WHERE parentID = '$id'");
 echo "<ul>";
 while ($row = mysql_fetch_array($result)) {
 echo "<li><a>{$row['ID']}</a>";
 $categories[] = $row['ID'];
 getChildren($categories, $row['ID']);
 echo "</li>";
 }
 echo "</ul>";
}
function getID($var) {
 $categories = array();
 getChildren($categories, $var);
 return $categories;
}

Here is the PHP reference page describing how to pass by reference instead of by value. Basically it says that any function parameter which has a & in front of it will be passed by reference instead of by value.

answered Mar 15, 2013 at 8:07
3
  • Same problem as with Naryls answer, if you do it this way you have to pass a reference to the original array (as fabs answer suggests) Commented Mar 15, 2013 at 8:14
  • oh, thanks. It works! Only one thing getChildren($categories, $var); should be $this->getChildren($categories, $var); Commented Mar 15, 2013 at 8:22
  • Oh, you have it in a class - that was not in the question. If course then you need the $this-> as well. Commented Mar 15, 2013 at 8:24
1

$categories only exists in the scope of get children. You could either pass it by reference as second parameter or create get children as a closure with use (&$categories)

answered Mar 15, 2013 at 8:09
0

function getChildren($id, $categories)

should work.

The array was out of scope, so the function didn't touch the one you are returning. It created a new local $categories inside the function.

answered Mar 15, 2013 at 8:06
1
  • 1
    This will return an empty array as well. The function getChildren() will work with a copy of the empty array, the original array will not be touched. If you want the function to add to the original array you have tu use function getChildren($id, &$categories);. Commented Mar 15, 2013 at 8:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.