Jump to content


Photo

PHP Navigation For CMS


  • Please log in to reply
7 replies to this topic

#1 scraptoft

scraptoft
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 22 June 2006 - 12:58 AM

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 Subcategory underneath it's speficific Category. Does anyone have any ideas how to do this, or what functions to use?

Thankyou for your time

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 22 June 2006 - 01:02 AM

Depends on how your data is related really. More than likely you would need to use recursion.

can we see your table schema?

#3 scraptoft

scraptoft
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 22 June 2006 - 01:17 AM

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.

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 22 June 2006 - 01:34 AM

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.

#5 scraptoft

scraptoft
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 22 June 2006 - 01:50 AM

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.

#6 scraptoft

scraptoft
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 22 June 2006 - 03:07 PM

bump

Anyone can shed some more light on this problem?

#7 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 22 June 2006 - 04:08 PM

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.


#8 scraptoft

scraptoft
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 22 June 2006 - 05:17 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users