Jump to content


Photo

Dynamic navigation help


  • Please log in to reply
6 replies to this topic

#1 icue

icue

    Member

  • Members
  • PipPip
  • 20 posts

Posted 20 January 2013 - 11:22 AM

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

#2 stijn0713

stijn0713

    Advanced Member

  • Members
  • PipPipPip
  • 231 posts

Posted 20 January 2013 - 03:17 PM

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';

#3 icue

icue

    Member

  • Members
  • PipPip
  • 20 posts

Posted 20 January 2013 - 07:42 PM

Hi, thanks for replying. I am just starting to learn php so I am not sure how to dynamically build the url. Could you perhaps give a short example.

Thanks,
Terrence

#4 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 20 January 2013 - 09:32 PM

I'm pretty sure he DID give an example. 

BTW, the idea of having all your pages on one page is ridiculous. 
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#5 icue

icue

    Member

  • Members
  • PipPip
  • 20 posts

Posted 22 January 2013 - 05:29 PM

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/mys...vel=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.

#6 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 23 January 2013 - 09:39 AM

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.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

#7 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 23 January 2013 - 10:49 AM

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>

Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com