Jump to content

Archived

This topic is now archived and is closed to further replies.

scraptoft

PHP Navigation For CMS

Recommended Posts

I am trying to create a navigation bar which will echo all of the categories in the database.

I want the navigation bar to show like below:

Category1
-subcategory
-subcategory
-subcategory
Category2
-subcategory
-subcategory
Category3
-subcategory
-subcategory
-subcategory
-subcategory

Heres my code to query Category:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$query_category = "SELECT name FROM webstructure where level = '2'";
$result_level = mysql_query($query_category)
or die("Invalid query: " . mysql_error());
while ($row=mysql_fetch_array($result_lcategory2)) {
$name = $row['name'];[/quote]

This echos like:
Category1
Category2
Category3

I cannot figure out how to echo the correct [b]Subcategory[/b] underneath it's speficific [b]Category[/b]. Does anyone have any ideas how to do this, or what functions to use?

Thankyou for your time

Share this post


Link to post
Share on other sites
Depends on how your data is related really. More than likely you would need to use recursion.

can we see your table schema?

Share this post


Link to post
Share on other sites
My category table is like so.

Name [name of category or subcategory]
parent_category [if its a subcategory the parent name is here]
level ['1','2','3'] [different levels to show on each page]

After writing this out, i'm starting to think I need to make changes to my table. Or can I get away with it this way?

Cheers for your fast reply.

Share this post


Link to post
Share on other sites
You could get away with it but there are better ways to represent hierarchical data. Maybe [a href=\"http://www.phpriot.com/d/articles/php/application-design/nested-trees-1/index.html\" target=\"_blank\"]this[/a] tutorial will help.

Share this post


Link to post
Share on other sites
I'm still trying to learn PHP but could do with this project finished (it doesn't have to be perfect) so I can get onto studying the language more. Could you give me a few pointers on how I could get away with it.

p.s thanks for the tutorial I will deffinatly read it more indepth in the morning.

Share this post


Link to post
Share on other sites
Check out my post here:

[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=94066&st=0&p=376462&#entry376462\" target=\"_blank\"]Catagories and Subcatagories[/a]

This has helped a few others out and should get you going the way you want.

Share this post


Link to post
Share on other sites
Cheers mate, i'm finding that post very helpful. I'm 80% to the way I want it, another hour or so and it should be working great.

Cheers guys

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.