homepage Welcome to WebmasterWorld Guest from 54.205.99.71
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Urgent Help Need - same function inside function but create array for
created a function and need help
sweetguyzzz




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

Hi,

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
-----2)Laptops
-----3)Keyboards
4) Mobiles
-----5)Samsung
-----6)Sony

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(
array("Computers",1),
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:

array(
array("Computers",1),
array("Laptops",2),
array("Keyboards",3),
and so on arrays...
)



Thanks in Advance.
Please help me as it is urgent.

 

jspeed




msg:4295647
 7:37 pm on Apr 11, 2011 (gmt 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?

e.g.

<ol>

// begin parent loop
<li>Parent
<ol>
// begin child loop
<li></li>
// end child loop
</ol>
// end parent loop
</li>

</ol>

Let me know if that makes sense.

sweetguyzzz




msg:4295960
 4:19 am on Apr 12, 2011 (gmt 0)

Hi,

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:

1)Computers
---2)Laptops
------3)Dell
------4)HP
------5)Others
---6)Monitors
------7)Sony
------8)ValueVision
---7)Mice


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

Thanks

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved