Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 06-12-2008, 08:43 AM create a menu
abhi's Avatar
Super Talker

Posts: 100
Name: abhishek
Location: mauritius
Trades: 0
Hi,
I have a 2 tables: category, subCategory
Can anyone explained me how to create a menu like a tree menu except there won't be any collapsible feature.
for example

Food(category)
meat(sub)
fruits(sub)
vegetables(sub)

Furniture(category)
chair(sub)
tables(sub)
bed(sub)

as you see, I want to display the category and under the category, I want to display the sub. I am using a database for that so that I can put other items without going through the code.
I tried a lot but couldn't do it. Any help will be grateful. Thanks a lot

Last edited by abhi; 06-13-2008 at 02:29 AM..
abhi is offline
Reply With Quote
View Public Profile Visit abhi's homepage!
 
 
Register now for full access!
Old 06-12-2008, 11:04 AM Re: create a menu
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
A deep Array would be the best way, for example.
PHP Code:
<?php
// Set the array
$catgorys[0][title] = "Food";
    
$catgorys[0][sub][0] = "meat";
    
$catgorys[0][sub][1] = "fruits";
$catgorys[1][title] = "Furniture";
    
$catgorys[1][sub][0] = "chair";
    
$catgorys[1][sub][0] = "tables";

// Now if you want to see them:
foreach($catgorys as $keys => $value){
    echo  
$value[title]."\n";
        foreach(
$value[sub] as $key => $vale){
            echo 
"\t".$vale."\n";
        }
    echo 
"\n"."\n";
}  
?>
The above would produce:
Code:
Food
	meat
	fruits

Furniture
	tables
So if you want to pull the above from a database, you just need a similar structure.
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

Last edited by rogem002; 06-12-2008 at 11:15 AM..
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 06-12-2008, 02:29 PM Re: create a menu
Arenlor's Avatar
Ultra Talker

Posts: 462
Name: Jerod Lycett
Location: /home/arenlor
Trades: 0
Alright, using generic names 'cat' and 'sub' for the columns.
PHP Code:
<html><head><title>Title</title></head><body>
<?php $cat="Food";
$query "SELECT * FROM `table` where cat=$cat";
$result mysql_query($query);
$cat_arr mysql_fetch_array($result);
echo 
"<p>".$cat."</p>";?>
<ul>
<?php foreach($cat_arr as $value){
echo 
"<li>".$value."</li>";}?>
</ul></body></html>
__________________
PHP Code:
<?php echo "Hello World"?>
HTML Code:
<html><head><title>Hello World</title></head><body><p>Hello World</p></body></html>
Arenlor is offline
Reply With Quote
View Public Profile Visit Arenlor's homepage!
 
Old 06-15-2008, 04:39 PM Re: create a menu
Junior Talker

Posts: 3
Trades: 0
First OP I would drop the category/subcategory and just go with a single array and associate each element with a parent via a parent ID.

Storing your data in a database for instance you schema might look like:

categories:
pkid, parent, name

The data would resemble something like:

1, 0, Food
2, 1, Meat
3, 1, Cheese
4, 1, Potatoes

The second field (parent ID) is what you use to relate categories with each other, so a ZERO value would indicate that the element is a root item and has no parent.

Food
Meat
Cheese
Potatoes

Your depth is now arbitrary, you just need to update relation ID's
Hockey is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to create a menu
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.84566 seconds with 12 queries