Jump to content

dynamic site structure and menu


defborn

Recommended Posts

Hello

I 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.

Link to comment
Share on other sites

[!--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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[!--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 :)
Link to comment
Share on other sites

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.php
then include menu.php on all pages.

[code]
<?php

include '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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.