Matt B Posted April 15, 2011 Share Posted April 15, 2011 Hi, I have been told it is possible to use PHP to design a left navigation bar which features a submenu for each link clicked on. So if you click "shoes" in the left navigation bar it will then display all the left navigation bar links but below shoes it might have a sub menu saying "Size 5", "Size 6", "Size 7", "Size 8", "Size 9", "Size 10". What function in PHP allows me to include submenus? I think I need to define a 'Foreign Key' and use categories but I do not know what function to use to alter the menu depending on what the user clicks/selects??! Any ideas? Matt. Quote Link to comment https://forums.phpfreaks.com/topic/233800-navigation-bar-that-shows-submenus-when-linked-is-clicked/ Share on other sites More sharing options...
spiderwell Posted April 15, 2011 Share Posted April 15, 2011 firstly I think some code of your current navigation might give us something to work on. I almost always make my navigation with list items <li>somepage link</li> A simple way to do it would be to have a querystring passing a value that triggers an include for the submenu <?php $product = $_GET['product']; ?> <ul> <li><a href="shoes.php?product=shoes">Shoes</a></li> <?php if ($product =="shoes") { // have submenu here, a nested list would work in this instance } ?> <li><a href="socks.php?product=shoes">Socks</a></li> </ul> Quote Link to comment https://forums.phpfreaks.com/topic/233800-navigation-bar-that-shows-submenus-when-linked-is-clicked/#findComment-1201958 Share on other sites More sharing options...
Muddy_Funster Posted April 15, 2011 Share Posted April 15, 2011 From what you just said in your post I'm gauging that this might be a little bit down the road from where you are at just now. It's nothing to do with "Foreign Keys" as they are a form of indexing in a database - nothing at all to do with PHP. Also, your flinging that word "function" around alot in contexts that make me think your not truly femiliar with what a PHP function really is. What you want to do is build up what is known as a menu "breadcrumb" trail. This is a section of code that follows users into the menu options and displays sub menus based on the current position. It can be done using arrays within the php code, or more commonly there is a refference field within the tables of the underlying database structure that is used to get the sub level information. Are you sure you want to go down this road? and if you are, which are you going to use : database or array? Quote Link to comment https://forums.phpfreaks.com/topic/233800-navigation-bar-that-shows-submenus-when-linked-is-clicked/#findComment-1201959 Share on other sites More sharing options...
Matt B Posted April 15, 2011 Author Share Posted April 15, 2011 I was thinking of using both a database and arrays. The array would feature products from the database. This might sound a bit cumbersome but I thought it would be smarter to keep all content in the database rather than at web pages. Perhaps you disagree? In the code example above, the 'if' function is used. Does this 'if' function mean 'if shoes is clicked'? I am a beginner and I thought PHP was only used to get information from a database to appear online (at a web page). Can it dynamically understand what is clicked using the 'if' function? Thanks, Matt. Quote Link to comment https://forums.phpfreaks.com/topic/233800-navigation-bar-that-shows-submenus-when-linked-is-clicked/#findComment-1201982 Share on other sites More sharing options...
spiderwell Posted April 15, 2011 Share Posted April 15, 2011 personally I wouldnt store a navigation in the database, it just generates extra database hits that dont need to be. if you look at my hlink, it passes a variable via the querystring called product and its value, in this instance shoes i have used a link with the page name of shoes.php, if you are a creating a 'one page shows all' type of script, then it would make more sense to be products.php?product=shoes, products.php?product=socks, etc etc. if you click on these links it will direct to the linked page passing the product via a querystring. the if statement can be used on its own in php, its not connected to the database/ database queries. in this example i pass the $_GET['product'] value into a variable called $product, ($_GET refers to the querystring, if it was a form it would be $_POST['product']) I then put an if statement to see if the $product value is equal to 'shoes', and if it is then show the sub menu for shoes. if I wanted it for socks too i would use the same format only to check for a value of 'socks' in the $product variable. nothing wrong with arrays, thats a great method, it will require more code to turn the array into the menu. Quote Link to comment https://forums.phpfreaks.com/topic/233800-navigation-bar-that-shows-submenus-when-linked-is-clicked/#findComment-1201986 Share on other sites More sharing options...
Matt B Posted April 15, 2011 Author Share Posted April 15, 2011 I understand that now. But how do I ensure the 'link clicked appears first with its' submenu'? If I had Socks Shoes Trousers Belts and I clicked belts I would want something like this to appear Belts 28" waist 30" waist 32" waist 34" waist Socks Shoes Trousers The if statement would not be enough to always include it at the top?! Would this sort of navigation benefit from passing more that 1 identity through the URL link? Thanks, Matt. Quote Link to comment https://forums.phpfreaks.com/topic/233800-navigation-bar-that-shows-submenus-when-linked-is-clicked/#findComment-1201990 Share on other sites More sharing options...
spiderwell Posted April 15, 2011 Share Posted April 15, 2011 you would need to add some sort of ordering function into your navigation script, the snippet I used wont do that. arrays can be re ordered so that might be the route to go down. Why not get to the stage of having the menu working with submenus and try the reordering bit after that? Quote Link to comment https://forums.phpfreaks.com/topic/233800-navigation-bar-that-shows-submenus-when-linked-is-clicked/#findComment-1201997 Share on other sites More sharing options...
Matt B Posted April 15, 2011 Author Share Posted April 15, 2011 I think I'll try what you suggest. But I have a query to ask: I am designing a template to be populated depending what link is clicked. So this would mean if you clicked: www.site.com/products.php?id=best-belt it would load the best belt page. But if I design the site the way you are suggesting I would need to send 'best-belt' and 'belts' through the URL so that 1) the best belt page loads and 2) the belts navigation menu appears - this would make the URL longer. Furthermore when I Mod Rewrite my URL using .htaccess file to something like www.site.com/best-belt I would need to miss out the 'belts' reference in the URL. Any ideas how to get around this potential problem? Matt. Quote Link to comment https://forums.phpfreaks.com/topic/233800-navigation-bar-that-shows-submenus-when-linked-is-clicked/#findComment-1201998 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.