defborn Posted May 15, 2006 Share Posted May 15, 2006 HelloI have to make this new website, without CMS or database, it's a corporate website. I want it to be a little bit dynamic, so I thought about throwing the website structure into an array. The problem is now where to go from here?Here is my array (the items are fictional, but represent a possible structure:[code]$menu = array("Home" => "home.php","products" => array ( "root" => "products.php", "Hardware" => array ( "root" => "products-hardware.php", "screens" => "products-hardware-screens.php", "desktops" => "products-hardware-desktops.php", "workstations" => "products-hardware-workstations.php", "Policy & practice" => "services-consulting-policy-practice.php" ), "Software" => array ( "root" => "products-software.php", "office" => "products-software-office.php", "management" => "products-software-management.php" ), "Whatever" => "products-whatever.php" ),"services" => array ( "support" => array ( "root" => "services-support.php", "general support" => "services-support-general-support.php", "no support" => "services-support-no-support.php" ), "catering" => array ( "root" => "services-catering.php", "Sandwiches" => "services-catering-sandwiches.php", "Weddings" => "services-catering-weddings.php" ), "Mechanic" => "services-mechanic.php" ));[/code]What I would like to do is build my main menu of the root items (home | products | services), but that I can do. Further when you click products, you should go to products.php off course, and there get a submenu of hardware, software and whatever. When clicking one of those .. well the idea is obvious. I thougth about getting a url (since I work with the one script serves all method) like this: [code]index.php?page=products&sub1=hardware&sub2=screens[/code]But I don't know if that is good, and even how I could get the values out of that querystring (I tried regular expressions, but all I get is the keys, not the values).Maybe my method is complete wrong, and I should use another approach. But what my goal was with this, is that when a new category comes, no matter what level it is in, all that has to be done for the developers using it afterwards is adding the physical page with the same naming convention, and adding an item to the array.Tnx for all the help. Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted May 15, 2006 Share Posted May 15, 2006 [!--quoteo(post=373992:date=May 15 2006, 09:04 AM:name=defborn)--][div class=\'quotetop\']QUOTE(defborn @ May 15 2006, 09:04 AM) [snapback]373992[/snapback][/div][div class=\'quotemain\'][!--quotec--]I thougth about getting a url (since I work with the one script serves all method) like this: [code]index.php?page=products&sub1=hardware&sub2=screens[/code]But I don't know if that is good, and even how I could get the values out of that querystring (I tried regular expressions, but all I get is the keys, not the values).[/quote]that's just the get method. you can access any variables in there from inside the $_GET array. so, $_GET['page'] will provide the value of "page" in the address.As for the arrays, I'm not quite sure where you're going with that. Quote Link to comment Share on other sites More sharing options...
macdumbpling Posted May 15, 2006 Share Posted May 15, 2006 use foreach() loops to build your menus. You can give the main sections a class characteristic and the subsections different class characteristics (for example, you could write it so the main sections are visible and the subsections are hidden and create a drop-down menu using javascript. or you could just set it up as a list style on the left side of the screen).you have to set up each part of the array the exact same for this script, so:"Home" => array( "root" => "home.php")"Products" => array( "root" => "products.php")[code]foreach($menu as $mainSection => $subSections){ foreach($subSections as $subSection => $values){ if($subSection == "root"){ // it's the main section's page value $strMenu .= "<a href=\"$values\">$mainSection</a>"; }else{ // it's another subsection if(is_array($values)){ foreach($values as $subSubSection => $sssValue){ $strMenu .= "<a href=\"$sssValue\">$subSection</a>"; } } } }}[/code]Okay...I'm done with that....I was doing this on the fly so it might not be entirely correct, but give it a shot and you can change the variable names (I'm not good at naming variables).I really HTH Quote Link to comment Share on other sites More sharing options...
defborn Posted May 15, 2006 Author Share Posted May 15, 2006 Sometimes I'm really amazed by the time and effort people put in these answers, but it's great!! Thanks a million! I'll try it out.[!--quoteo(post=374033:date=May 15 2006, 05:44 PM:name=macdumbpling)--][div class=\'quotetop\']QUOTE(macdumbpling @ May 15 2006, 05:44 PM) [snapback]374033[/snapback][/div][div class=\'quotemain\'][!--quotec--]use foreach() loops to build your menus. You can give the main sections a class characteristic and the subsections different class characteristics (for example, you could write it so the main sections are visible and the subsections are hidden and create a drop-down menu using javascript. or you could just set it up as a list style on the left side of the screen).you have to set up each part of the array the exact same for this script, so:"Home" => array( "root" => "home.php")"Products" => array( "root" => "products.php")[code]foreach($menu as $mainSection => $subSections){ foreach($subSections as $subSection => $values){ if($subSection == "root"){ // it's the main section's page value $strMenu .= "<a href=\"$values\">$mainSection</a>"; }else{ // it's another subsection if(is_array($values)){ foreach($values as $subSubSection => $sssValue){ $strMenu .= "<a href=\"$sssValue\">$subSection</a>"; } } } }}[/code]Okay...I'm done with that....I was doing this on the fly so it might not be entirely correct, but give it a shot and you can change the variable names (I'm not good at naming variables).I really HTH[/quote] Quote Link to comment Share on other sites More sharing options...
defborn Posted May 16, 2006 Author Share Posted May 16, 2006 [!--quoteo(post=374012:date=May 15 2006, 04:50 PM:name=michaellunsford)--][div class=\'quotetop\']QUOTE(michaellunsford @ May 15 2006, 04:50 PM) [snapback]374012[/snapback][/div][div class=\'quotemain\'][!--quotec--]As for the arrays, I'm not quite sure where you're going with that.[/quote]Well, I don't know if it's the best way to do it, but I want my site structure to be editable in one file, say they want to add 2 categories, then they will know to add the 2 physical pages, and update the arrays with the new values.I don't see another way right now... But I am open for improvements :) Quote Link to comment Share on other sites More sharing options...
haydndup Posted May 16, 2006 Share Posted May 16, 2006 Have you considered using the include ()?Put you menu arrays, or whatever you are doing to set up the navigation structure in a file. eg menu.phpthen include menu.php on all pages.[code]<?phpinclude 'menu.php';?>[/code]I use something similar on my websites, i have a top.php, left.php, and bottom.php and the site layout is distributed between the three files, allowing me to quickly and easily change layout etc Quote Link to comment 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.