Hey guys, can anyone tell me what is going wrong with this script?
//This function retrieves all of the parents of a category
function category_family($category_id)
{
global $parent_category_array;
//Select the child category that we want to retrieve the parents for
$query = 'select * from item_categories where item_category_id = "' . $category_id . '"';
$result = mysql_query($query);
$a_parent_category = mysql_fetch_assoc($result);
//If we found a parent category
if(is_array($a_parent_category))
{
echo 'Found [' . $a_parent_category['item_category_name'] . ']<br />';
//Record it.
$parent_category_array[] = $a_parent_category;
//Check again to see if we will find another (recursively call itself again until no more have been found)
category_family($a_parent_category['item_category_parent']);
} else { //If we didn't managed to find a parent category
//Even though we didn't find a parent category this time, it's possible we found one before
if(is_array($parent_category_array))
{
echo 'Exiting with an array';
//If we found at least one category, return the array.
return $parent_category_array;
} else {
echo 'Exiting with out an Array';
//If we never found one category ever, just return false.
return false;
}
}
}
//Load all of the parents
$parents = category_family(2907);
print_r($parents);
The output of this script is as follows.
Found [Sub Sub Category]
Found [Sub Category]
Found [Main Category]
Exiting with an array.
Ok, so what I’m gathering is that the function is indeed locating parent categories. That it is exiting while it knows that it’s found parent categories yet for some reason I am unable to retrieve the category array from the function when I try to create an array called “$parents” [ $parents = category_family(2907); ]
You can’t call a function inside the array square bracket construct.
And even if you could, won’t category_family return an array? An array can’t be the key for an array element
LOL. I’m aware of that. The reason I used [square brackets] was because rogue smiley faces kept coming up. The actual script is true to what is in the first Code Block I posted.
I just tried replacing print_r($parents) with var_dump($parents) and it says NULL
//If we found a parent category
if(is_array($a_parent_category))
{
echo 'Found [' . $a_parent_category['item_category_name'] . ']<br />'; //Record it.
$parent_category_array[] = $a_parent_category;
//Check again to see if we will find another (recursively call itself again until no more have been found)
category_family($a_parent_category['item_category_parent']);
}
Ok, rebooted to my work drive and I was thinking about what you suggested but now that I’m actually here about to write the code I’m suddenly confused how I would do what you suggested.
If this is running:
echo 'Exiting with an array';
Doesn’t that mean that the script is indeed ending there and not in the above statement you suggested?
Ok I’ve re-written this slightly and I’m very confused now as everything in the script appears as though it should be working.
Here is the code now. Note the print_r($parent_category_array) ; in the exiting return statement.
//This function retrieves all of the parents of a category
function category_family($category_id)
{
global $parent_category_array;
//Select the child category that we want to retrieve the parents for
$query = 'select * from item_categories where item_category_id = "' . $category_id . '"';
$result = mysql_query($query);
$a_parent_category = mysql_fetch_assoc($result);
//If we found a parent category
if(is_array($a_parent_category))
{
echo 'Found [' . $a_parent_category['item_category_name'] . ']<br />';
//Record it.
$parent_category_array[] = $a_parent_category;
//Check again to see if we will find another (recursively call itself again until no more have been found)
category_family($a_parent_category['item_category_parent']);
} else { //If we didn't managed to find a parent category
//Even though we didn't find a parent category this time, it's possible we found one before
if(is_array($parent_category_array))
{
echo 'This is the Array being echo`d from the function.<br />';
print_r($parent_category_array);
//If we found at least one category, return the array.
return $parent_category_array;
} else {
echo 'Exiting with out an Array<br />';
//If we never found one category ever, just return false.
return false;
}
}
}
//Load all of the parents
$parents = category_family(2907);
echo '<br />This is the Array being echo`d from outside of the function.<br />';
print_r($parents);
And the results show almost what I’m expecting.
Found [Sub Sub Category]
Found [Sub Category]
Found [Main Category]
This is the Array being echod from the function. Array ( [0] => Array ( [item_category_id] => 2907 [item_type_id] => 1 [item_category_name] => Sub Sub Category [item_category_desc] => [item_category_parent] => 2906 [item_category_order] => 0 [date_created] => 1326071099 ) [1] => Array ( [item_category_id] => 2906 [item_type_id] => 1 [item_category_name] => Sub Category [item_category_desc] => [item_category_parent] => 2767 [item_category_order] => 0 [date_created] => 1326071092 ) [2] => Array ( [item_category_id] => 2767 [item_type_id] => 1 [item_category_name] => Main Category [item_category_desc] => Here is a description. [item_category_parent] => 0 [item_category_order] => 1 [date_created] => 1312659389 ) ) This is the Array being echod from outside of the function.
As you can see the Array can be loaded and echo’d correctly from inside of the function right before exit. But the function doesn’t pass the array to $parents… Isn’t this weird?
I think you must be executing different code, perhaps a different version of the file to what you think you are.
Your output has both the "Found [Sub Sub Category]" type statments and also “This is the Array being echo’d from the function” But those echo statements are in mutually exclusive if else blocks. I don’t see how they can both be output.
Also doesn’t mysql_fetch_assoc($result); produce an empty array if no results were found?
So the next line of if(is_array($a_parent_category)) will always be true?
Mmm, I don’t think so. As I just adjusted this file to alter the output, which it has. That tells me that I am editing the right file. Also the function is declared right above where I actually call it on the same script just as I have portrayed it here in the forums.
The function calls itself. I believe this is called Recursive functionality. So it echo’s out that it’s found a parent category and then it runs itself again until it doesn’t find another parent category. See below. I know this code block is running. That’s where I am getting the output for found parent categories.
echo 'Found [' . $a_parent_category['item_category_name'] . ']<br />';
//Record it.
$parent_category_array[] = $a_parent_category;
//Check again to see if we will find another (recursively call itself again until no more have been found)
category_family($a_parent_category['item_category_parent']);
No they do not. At least not on my version of PHP. At any rate, the problem isn’t that it’s falsifying an Array. The problem is that it’s returning the array that it’s building correctly.
I’ve tinkered a bit around with a global array, and your code seems ok from what i got from the tinkering.
Why not try an optional solution?
Instead of returning the global array, why not return a boolean which tells you if there was any hits?
IF there is hits, print out the global variable, if not, tell the user there were no hits?
Oh, I forgot to update this forum post. I actually sold the problem. All I had to do was move the Return $array statement to right below the recursive function statement. I didn’t realize that when you recursively call functions inside of themselves they can only return values from the initial instance of the function.