Jump to content

Dynamic navigation help


icue

Recommended Posts

Hi,

 

I have a problem that I am not quite sure how to solve.

 

I have a standard index.php page with header, footer and content area.

 

I use a titorial found online to include six pages into the content area

via a unordered list and a switch function.

 

Sample code below:

 

<ul class="topMenu">

<li><a href="?page=home">HOME</a></li>

<li><a href="?page=news">NEWS</a></li>

</ul>

 

<?PHP

switch($page)

{

case "home";

include 'home.php';

break;

case "news";

include 'news.php';

break;

default;

include 'home.php';

break;

}

?>

 

This all works well. The problem occurs when I try to link to

pages in an image galley with links like the following:

http://localhost/mys...evel=album&id=2

 

I don't know how or if it is possible to include my gallery pages

in my navigation.

 

Could someone please offer suggestions or sample scripts that

would point me in the right direction.

 

Also, is this a safe method to nagivate a site.

 

Thanks in advance,

 

Terrence

Link to comment
Share on other sites

Dynamic navigation means you are dynamically building the link to another page, as opposed to hardcoding the link. A reason could be because the link can contain different parameters, like having different id's passed to a certain script. So, if you want a dynamic link, you will need to dynamically build it.

 

e.g. $url = /home.php?id='$anID';

Link to comment
Share on other sites

Hi,

 

Sorry but I don't understand the example given. I am new to PHP and struggling quite a bit to get my head around this.

Perhaps someone could be a little more patient with me offer some more help.

 

To my original switch function, I added the following:

 

case "galleries";

include 'galleries.php';

break;

 

and us ?page=galleries&level=album&id=2 in a href to include the gallery page. This works ok and the gallery opens

showing the correct images. However, if I try to navigate in the gallery using ?page=Galleries&level=album&id=10, or

http://localhost/mysite/index.php?page=Galleries&level=album&id=10, from a link, I get directed back to the default

home page.

 

Is it possible to pass variables to the open gallery page to allow me to change the images shown in the gallery

from a link. If this is not possible, could someone suggest another option - hopefully in a way I can understand.

 

As for putting all my pages on one page. Tutorials I have read state that you can have an index page with a header file

and a footer file and just change the content with includes which what I am trying to do. Is this wrong?

 

Thanks in advance.

Link to comment
Share on other sites

Since you haven't shown the desired navigation. Do you want all the galleries to be listed in the main navigation, or after you click on a main GALLERY link, you want to replace the navigation with a list of the galleries and for either case, what defines your galleries? How many of them, what are the id's, ...?

 

Also, you should use an array (or a database table) to define your navigation, so that all you need to do is change the array to add, remove, or alter the navigation. Using a switch/case requires you to actually edit the program logic every time you want to change the navigation.

Link to comment
Share on other sites

For your main navigation (what you have shown in this thread), you would use something like the following -

 

<?php
// define main navigation (used to build navigation links and to validate the page that is requested)
$default_page = 'home';
$main_navigation['home'] = 'HOME';
$main_navigation['news'] = 'NEWS';
$main_navigation['galleries'] = 'GALLERIES';

// process page request and include the requested content
$page = isset($_GET['page']) ? strtolower(trim($_GET['page'])) : $default_page;
if(!isset($main_navigation[$page])){
   // invalid page requested, use default
   $page = $default_page;
}

include "$page.php";


// produce main (i.e. these go to the root page without any extra get parameters) navigation -
$main_nav = '';
foreach($main_navigation as $key=>$value){
   $main_nav .= "<li><a href='?page=$key'>$value</a></li>";
}


// output navigation in the actual HTML document -
?>
<ul class="topMenu">
<?php echo $main_nav; ?>
</ul>

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.