...

View Full Version : recursive funciton for categories and sub categories?



MRMAN
02-08-2006, 01:48 PM
Hello.
I have some code which i created that will get a list of all the main categories and then all the sub categories.
The data is sorted in an array and the sub categories are stored in an array in the original array.
let me show you



function getparent($parent)
{
$tmp = Array();
$getparent = "SELECT * FROM static_pages WHERE page_parent = $parent";
$parentquery = mysql_query($getparent) or die(mysql_error());

while($pageresult = mysql_fetch_array($parentquery))
{
array_push($tmp, $pageresult["page_id"]);

$tmp2 = getparent($pageresult["page_id"]);
if(count($tmp2) > 0)
{
array_push($tmp, $tmp2);
}
}
return $tmp;
}

print "<pre>";
print_r(getparent(0));
print "</pre>";


any it produces a array like this


Array
(
[0] => 1
[1] => 2
[2] => Array
(
[0] => 3
[1] => Array
(
[0] => 4
)

)

[3] => 5
[4] => 6
[5] => Array
(
[0] => 7
[1] => Array
(
[0] => 8
[1] => Array
(
[0] => 9
)

)

[2] => 10
)

)


Basically i can't see an easy way to navigate to page_id 9. I could go to key-5 and check to see if it an array. if it is then step into it and carry on until i find 9.

Does anyone know and easier way to find the sub pages (there could be any number of sub sub pages. and also do you know an easier way to store the data.

Cheers

MRMAN

Rich Pedley
02-08-2006, 01:52 PM
http://www.sitepoint.com/article/hierarchical-data-database/2

may be a useful starting point for you.

MRMAN
02-08-2006, 03:15 PM
Thank you very much



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum