Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-09-2012, 06:33 PM
|
#1
|
|
Member
Registered: Oct 2003
Location: Canada
Distribution: Fedore Core 4, Ubuntu 7.04, Suse 10.2
Posts: 376
Rep:
|
PHP - Return Array from a Function issues... GRR!
Hey guys, can anyone tell me what is going wrong with this script?
Code:
//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.
Quote:
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)
What the HELL am I missing here?
Last edited by wh33t; 01-11-2012 at 04:53 PM.
Reason: SOLVED !
|
|
|
|
01-10-2012, 04:04 AM
|
#2
|
|
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,624
|
I dunno if this is the issue, but you're certainly mixing global variables with local ones (local ones being preferred). What happens if you add
Code:
print_r($parent_category_array);
?
I would be more happy if each time you returned from category_family() you returned an array or false (which you do) but then didn't update the $parent_category_array at all.
You would just call
Code:
return array_merge(category_family(blah),$a_parent_category);
when returning (or similar, I'm at school so can't check code  )
Hope this helps,
|
|
|
|
01-10-2012, 09:21 AM
|
#3
|
|
Member
Registered: May 2004
Location: France
Distribution: Slackware Gentoo
Posts: 157
Rep:
|
just had a quick look , but I would say that your condition
Code:
if(is_array($parent_category_array))
will always be true even when you don't get a result from the SQl
as you declared an empty array before
|
|
|
|
01-10-2012, 11:07 PM
|
#4
|
|
Member
Registered: Oct 2003
Location: Canada
Distribution: Fedore Core 4, Ubuntu 7.04, Suse 10.2
Posts: 376
Original Poster
Rep:
|
Hey guys, I appreciate the replies. I have since altered the script to further investigate where I'm going wrong here. Here is the new code.
Code:
//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);
This is it's output now.
Quote:
Found [Sub Sub Category]
Found [Sub Category]
Found [Main Category]
This is the Array being echo`d 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 echo`d from outside of the function.
|
So from what I gather. EVERYTHING is working as I expect it to. The function is calling itself recursively as it should. When it fails to find any more Parent Categories it is exiting the function EXACTLY where I expect it too. " This is the Array being echo`d from the function." is running from:
Code:
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;
I also notice it is indeed printing out the $parent_category_array right in the exit statement. The problem appears to be with returning it! GRR! What is going on! Is that not how you return an array from a function? Shouldn't this work:
Code:
function make_an_array()
{
$array_name[0] = 'some value';
$array_name[1] = 'some other value';
return $array_name;
}
$new_array_from_a_function = make_an_array();
|
|
|
|
01-11-2012, 12:24 AM
|
#5
|
|
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
|
Quote:
Originally Posted by wh33t
Code:
category_family($a_parent_category['item_category_parent']);
|
When you do the above recursive call, you discard the return value!
So, if you have nested results (and you do), the very first recursive call will just drop the results on the floor and return nothing. Note that after that call no return statement will be executed.
Consider this recursive example.
Code:
function babushka($depth, $array = FALSE)
{
if ($depth > 1)
$array = babushka($depth - 1, $array);
if ($array === FALSE || $array === NULL)
$array = array();
else
if (!is_array($array))
$array = array( $array );
return array( 'depth' => $depth, 'array' => $array );
}
var_dump(babushka(1));
var_dump(babushka(5, array("One", "Two"));
var_dump(babushka(4, babushka(3, babushka(2, array("1", "0")))));
Your make_an_array() example function does work, if you first initialize $array_name to an empty array, i.e.
Code:
function make_an_array()
{
$array_name = array();
$array_name[0] = 'some value';
$array_name[1] = 'some other value';
return $array_name;
}
although a much simpler way to write the same function would be
Code:
function make_an_array()
{
return array( 0 => 'some value',
1 => 'some other value' );
}
Hope this helps.
|
|
|
|
01-11-2012, 01:42 AM
|
#6
|
|
Member
Registered: Oct 2003
Location: Canada
Distribution: Fedore Core 4, Ubuntu 7.04, Suse 10.2
Posts: 376
Original Poster
Rep:
|
I get it now. Thanks for your help man.
The problem is that I was assuming that when I recursive called the function that it wouldn't run any more statements after.
This now works as expected.
PHP Code:
//This function retrieves all of the parents of a category
function category_family($category_id)
{
global $parent_category_array, $run_times;
//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);
$run_times++;
//If we found a parent category
if(is_array($a_parent_category))
{
//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']);
return $parent_category_array;
} else { //If we didn't managed to find a parent category
//If we didn't find a parent.
return false;
}
}
//Load all of the parents
$parents = category_family(2907);
print_r($parents);
|
|
|
|
01-11-2012, 10:48 AM
|
#7
|
|
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,624
|
Well done, if you don't have any further questions can you mark this thread as SOLVED please? (and mark e.g. Nominal Animal's post as being 'helpful', as I'm guessing it probably was  )
Thanks,
|
|
|
1 members found this post helpful.
|
01-11-2012, 04:52 PM
|
#8
|
|
Member
Registered: Oct 2003
Location: Canada
Distribution: Fedore Core 4, Ubuntu 7.04, Suse 10.2
Posts: 376
Original Poster
Rep:
|
Will do. Thank you LQ!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:10 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|