Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Urgent Help Need - same function inside function but create array for

created a function and need help

6:20 pm on Apr 11, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:May 30, 2009
posts: 71
votes: 0


I am trying to create a function which will be able to fetch record from the database but the problem is the data is to be arrange in tree form. Means I to arrange my categories like below:

1) Computers
4) Mobiles

Now assume the bullet numbering of these categories as their id in database and parent id of 1 and 4 number is null but of 2, 3, 5 and 6 as their above.

Function I am using is below:
function CategoryForListItem($parent_id = "") {
$sql = empty($parent_id) ? "SELECT cat_id, cat_name, cat_level FROM category WHERE cat_parent_id IS NULL" : "SELECT cat_id, cat_name, cat_level FROM category WHERE cat_parent_id='$parent_id'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$category_list_array[] = array($row['cat_name'],$row['cat_id']);
CategoryForListItem($row['cat_id']); //calling same function
return $category_list_array;

This Function Returns:
It only returns the values from first while loop that is only return two loops, first is array("Computers", 1) and second is array("Mobiles", 4) . Like this
array("Mobiles", 4)

My Aim To Get:
I want an array from this function and array is multidimensional array with all categories values with their id as given below:

and so on arrays...

Thanks in Advance.
Please help me as it is urgent.
7:37 pm on Apr 11, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 13, 2007
votes: 0

I may be missing the big picture here, but couldn't you just fetch the parent category in a loop, then within that loop fetch the child categories, all just using an ordered list?



// begin parent loop
// begin child loop
// end child loop
// end parent loop


Let me know if that makes sense.
4:19 am on Apr 12, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:May 30, 2009
posts: 71
votes: 0


Yes, you are write, I want to do like this but problem is here I want to create a categories list on unknown number of levels like below:


And if there is something inside it, I want it.


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members