icue Posted January 20, 2013 Share Posted January 20, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/273393-dynamic-navigation-help/ Share on other sites More sharing options...
stijn0713 Posted January 20, 2013 Share Posted January 20, 2013 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'; Quote Link to comment https://forums.phpfreaks.com/topic/273393-dynamic-navigation-help/#findComment-1407126 Share on other sites More sharing options...
icue Posted January 21, 2013 Author Share Posted January 21, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/273393-dynamic-navigation-help/#findComment-1407176 Share on other sites More sharing options...
Jessica Posted January 21, 2013 Share Posted January 21, 2013 I'm pretty sure he DID give an example. BTW, the idea of having all your pages on one page is ridiculous. Quote Link to comment https://forums.phpfreaks.com/topic/273393-dynamic-navigation-help/#findComment-1407193 Share on other sites More sharing options...
icue Posted January 22, 2013 Author Share Posted January 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273393-dynamic-navigation-help/#findComment-1407586 Share on other sites More sharing options...
PFMaBiSmAd Posted January 23, 2013 Share Posted January 23, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273393-dynamic-navigation-help/#findComment-1407737 Share on other sites More sharing options...
PFMaBiSmAd Posted January 23, 2013 Share Posted January 23, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/273393-dynamic-navigation-help/#findComment-1407748 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.