Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Breadcrumbs

Status
Not open for further replies.

irbk

MIS
Oct 20, 2004
578
US
I'm building a website using PHP & MySQL. Website will have a top level menu, with the possibility of 2 submenu's. I'd like the top of the page to have breadcrumbs showing where you are, for example something like:

Websites >> Dynamic >> PHP

In the past, I've created a database filed that was breadcrumbs and they were just manually added. This can lead to the breadcrumbs being incorrect, so I'm hoping for more of a "foolproof" method of creating the breadcrumbs. I'm not against my entire menu system being database driven, though I have to admit, I'm not sure how one would design that database structure.
I'm still in the database design stage of the site, and I'm hoping some one could point me in a good direction for getting the breadcrumbs to be more "foolproof" and if that involves having the entire menu system database driven, all the better.

Thanks in advance for the advice!
 
My CSS can only go 4 levels deep before breaking (the site currently only needs 3 levels deep, so I only added the 4th level for expandability) I'm thinking perhaps something like this:
Table 1:
L1Menu
ID = Primary Key
Name = Text displayed as the link
Link = The URL where you will be taken too when clicked
Position = For ordering the menu
HasSubMenu = If the menu item has a sub menu or not

L2Menu
ID = Primary Key
Name = Text displayed as the link
Link = The URL where you will be taken too when clicked
Position = For ordering the menu
ParentID = L1Menu.ID of the menu this is a submenu to
HasSubMenu = If this menu item has a sub menu or not

L3Menu
ID = Primary Key
Name = Text displayed as the link
Link = The URL where you will be taken too when clicked
Position = For ordering the menu
ParentID = L2Menu.ID of the menu this is a submenu to
HasSubMenu = If this menu item has a sub menu or not

L4Menu
ID = Primary Key
Name = Text displayed as the link
Link = The URL where you will be taken too when clicked
Position = For ordering the menu
ParentID = L4Menu.ID of the menu this is a submenu to

The sudocode to display the menus would be something like
$L1MenuQuery = Select * from L1Menu ORDER BY Position
While ($L1MenuQuery)
echo "<li>......."
If HasSubMenu = True
$L2MenuQuery = Select * from L2Menu where L2Menu.ParentID = L1MenuQuery.ID ORDER BY Position
While (L2MenuQuery)
echo "<li>......."
If HasSubMenu = True
etc, etc, etc though all my levels of menus

Sound resonable or am I making it way more complicated then it needs to be?

Sound good?



 
Here is my actual code
Code:
<?php
				$l1menuquery = "SELECT * FROM l1menu ORDER BY position ASC";
				$l1menuresult = mysql_query ($l1menuquery);
				if ($l1menuresult) { // if the query is good, display the Menu items
					while ($l1menu = mysql_fetch_array ($l1menuresult, MYSQL_ASSOC)) {
						echo'<li>';
						echo'  <a href="' . $l1menu['link']. '">' . $l1menu['name']. '</a>';
						if ($l1menu['hassubmenu'] == "1") {
							$l2menuquery = "SELECT * from l2menu where parentid = '".$l1menu['id']."' ORDER BY position ASC";
							$l2menuresult = mysql_query ($l2menuquery);
							if ($l2menuresult) { // if the level 2 menu query is good, display the level 2 menus
								echo'<ul>';
								while ($l2menu = mysql_fetch_array ($l2menuresult, MYSQL_ASSOC)) {
									echo'<li>';
									echo'  <a href="' . $l2menu['link']. '">' . $l2menu['name']. '</a>';
									if ($l2menu['hassubmenu'] == "1") {
										$l3menuquery = "SELECT * from l3menu where parentid = '".$l2menu['id']."' ORDER BY position ASC";
										$l3menuresult = mysql_query ($l3menuquery);
										if ($l3menuresult) { // if the level 3 menu query is good, display the level 3 menus
											echo'<ul>';
											while ($l3menu = mysql_fetch_array ($l3menuresult, MYSQL_ASSOC)) {
												echo'<li>';
												echo'  <a href="' . $l3menu['link']. '">' . $l3menu['name']. '</a>';
												if ($l3menu['hassubmenu'] == "1") {
													$l4menuquery = "SELECT * from l4menu where parentid = '".$l3menu['id']."' ORDER BY position ASC";
													$l4menuresult = mysql_query ($l4menuquery);
													if ($l4menuresult) { // if the level 4 menu query is good, display the level 4 menus
														echo'<ul>';
														while ($l4menu = mysql_fetch_array ($l4menuresult, MYSQL_ASSOC)) {
															echo'<li>';
															echo'  <a href="' . $l4menu['link']. '">' . $l4menu['name']. '</a>';
															echo'</li>';
														}
														echo'</ul>';
													}
												}
												echo'</li>';
											}
											echo'</ul>';
										}
									}
									echo'</li>';
								}
								echo'</ul>';
							}
						}
						echo'</li>';
						echo "\n";
					}
				}		
			?>
It works, don't know if it was the best way to go about it, but it works!
 
*giggle* ok, while the above code works great for dynamically pulling up to 4 levels of menus, I've just figured out it doesn't help at all for breadcrumbs. So I'm kind of in the same situation as before.
 
One more thing, I need the breadcrumbs to NOT be based off of the current folder structure as the site itself won't have that to go by becuase the entire site will be run off of only about 7 different PHP pages. So I can't just use a script that expands the current path because the current path would say I'm at the same level of "home" rather then (using my earlier example) in
Websites >> Dynamic >> PHP
in this example, the page that I would be on would be websites.php. Both the "Dynamic" and "Php" pages would be displayed on websites.php page (differnt results from the DB query) and the "structure" of where the "PHP" page is (meaning that the "php" page is under the heading "dynamic" which is under the heading "websites") is purely virtual and if I had the overwhelming want, I could have "Dynamic" be the sub and "php" be the parent. I hope this makes since.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top