healthbasics Posted June 15, 2010 Share Posted June 15, 2010 My index.php <?php include "menu.inc.php"; function menu(){ global $_menu; $str = ''; foreach($_menu as $url => $name){ $sel = ($_GET['page'] == $url ) ? ' id="selected" ' : ''; $str .= "<li ". $sel ."><a href='index.php?page=" . $url . "' >" . $name . "</a>\n"; } echo $str; } $page = ( isset($_GET['pg']) ? $_GET['pg'] : ((isset($_GET['page']))? $_GET['page']:'home')); $file = basename($page).'.php'; $html_path = "php/"; ?> and it calls the menu function like this: <? #include $html_path."menu.html"; menu(); ?> my menu.inc.php: <? $_menu = array('home' => 'Philosophy & Focus', 'biography' => 'Biography', 'consultations' => 'Consultations', 'classes' => 'Classes', 'client_comments' => 'Client Comments', 'calendar' => 'Calendar', 'recommended_reading' => 'Recommended Reading', 'products' => 'Products', ); i tried a line like 'http://www.google.com/' => 'Google', But it gives me this: http://www.mysite.net/index.php?page=http://www.google.com instead which incidentally produces a blank page instead of a 404 error. In general www.mysite.net/asdfkdlsda gives a 404 but www.mysite.net/index.php?page=dsajlkfdj gives just a blank page. Another minor little thing I need to fix... Thanks. Link to comment https://forums.phpfreaks.com/topic/204857-how-can-i-use-this-menu-function-to-link-to-external-urls/ Share on other sites More sharing options...
hcdarkmage Posted June 15, 2010 Share Posted June 15, 2010 Simple way to combat that: <?php include "menu.inc.php"; function menu(){ global $_menu; $str = ''; foreach($_menu as $url => $name){ $sel = ($_GET['page'] == $url ) ? ' id="selected" ' : ''; $str .= "<li ". $sel ."><a href='index.php?page=" . $url . "' >" . $name . "</a>\n"; //<--- This line sets the URL in the menu } echo $str; } $page = ( isset($_GET['pg']) ? $_GET['pg'] : ((isset($_GET['page']))? $_GET['page']:'home')); $file = basename($page).'.php'; $html_path = "php/"; ?> and it calls the menu function like this: <? #include $html_path."menu.html"; menu(); ?> my menu.inc.php: <? $_menu = array('home' => 'Philosophy & Focus', //<--- These are the links that it calls. 'biography' => 'Biography', 'consultations' => 'Consultations', 'classes' => 'Classes', 'client_comments' => 'Client Comments', 'calendar' => 'Calendar', 'recommended_reading' => 'Recommended Reading', 'products' => 'Products', ); Simply change the line I pointed out to: $str .= "<li ". $sel ."><a href=" . $url . " >" . $name . "</a>\n"; And change the menus to: 'index.php?page=biography' => 'Biography', and set the Google menu to: 'http://www.google.com' => 'Google', And you should be able to set up any other external links that you like. Link to comment https://forums.phpfreaks.com/topic/204857-how-can-i-use-this-menu-function-to-link-to-external-urls/#findComment-1072412 Share on other sites More sharing options...
KevinM1 Posted June 15, 2010 Share Posted June 15, 2010 Also, never, ever, ever use the 'global' keyword. It's a lazy way to code, which often leads to nasty, unexpected surprises down the road. Do it right and pass $_menu in through your function's argument list. In other words, instead of: function menu() { global $_menu; // ... } Do: function menu($_menu) { // ... } It doesn't seem like much, but trust me, it matters in the long run. Link to comment https://forums.phpfreaks.com/topic/204857-how-can-i-use-this-menu-function-to-link-to-external-urls/#findComment-1072415 Share on other sites More sharing options...
healthbasics Posted June 15, 2010 Author Share Posted June 15, 2010 Simple way to combat that: <?php include "menu.inc.php"; function menu(){ global $_menu; $str = ''; foreach($_menu as $url => $name){ $sel = ($_GET['page'] == $url ) ? ' id="selected" ' : ''; $str .= "<li ". $sel ."><a href='index.php?page=" . $url . "' >" . $name . "</a>\n"; //<--- This line sets the URL in the menu } echo $str; } $page = ( isset($_GET['pg']) ? $_GET['pg'] : ((isset($_GET['page']))? $_GET['page']:'home')); $file = basename($page).'.php'; $html_path = "php/"; ?> and it calls the menu function like this: <? #include $html_path."menu.html"; menu(); ?> my menu.inc.php: <? $_menu = array('home' => 'Philosophy & Focus', //<--- These are the links that it calls. 'biography' => 'Biography', 'consultations' => 'Consultations', 'classes' => 'Classes', 'client_comments' => 'Client Comments', 'calendar' => 'Calendar', 'recommended_reading' => 'Recommended Reading', 'products' => 'Products', ); Simply change the line I pointed out to: $str .= "<li ". $sel ."><a href=" . $url . " >" . $name . "</a>\n"; And change the menus to: 'index.php?page=biography' => 'Biography', and set the Google menu to: 'http://www.google.com' => 'Google', And you should be able to set up any other external links that you like. Cool thanks. I will try this out when I fix all the other bugs I'm chasing. While I was playing I made a significantly less elegant fix of making a file google.php with a php redirect to www.google.com Link to comment https://forums.phpfreaks.com/topic/204857-how-can-i-use-this-menu-function-to-link-to-external-urls/#findComment-1072424 Share on other sites More sharing options...
healthbasics Posted June 15, 2010 Author Share Posted June 15, 2010 Also, never, ever, ever use the 'global' keyword. It's a lazy way to code, which often leads to nasty, unexpected surprises down the road. Do it right and pass $_menu in through your function's argument list. In other words, instead of: function menu() { global $_menu; // ... } Do: function menu($_menu) { // ... } It doesn't seem like much, but trust me, it matters in the long run. Thanks for the tip. I will try this. Link to comment https://forums.phpfreaks.com/topic/204857-how-can-i-use-this-menu-function-to-link-to-external-urls/#findComment-1072425 Share on other sites More sharing options...
healthbasics Posted June 15, 2010 Author Share Posted June 15, 2010 Also, never, ever, ever use the 'global' keyword. It's a lazy way to code, which often leads to nasty, unexpected surprises down the road. Do it right and pass $_menu in through your function's argument list. In other words, instead of: function menu() { global $_menu; // ... } Do: function menu($_menu) { // ... } It doesn't seem like much, but trust me, it matters in the long run. Making this suggested change breaks it. The whole link menu just disappears. Any ideas on fixing my 404? Link to comment https://forums.phpfreaks.com/topic/204857-how-can-i-use-this-menu-function-to-link-to-external-urls/#findComment-1072434 Share on other sites More sharing options...
KevinM1 Posted June 15, 2010 Share Posted June 15, 2010 Also, never, ever, ever use the 'global' keyword. It's a lazy way to code, which often leads to nasty, unexpected surprises down the road. Do it right and pass $_menu in through your function's argument list. In other words, instead of: function menu() { global $_menu; // ... } Do: function menu($_menu) { // ... } It doesn't seem like much, but trust me, it matters in the long run. Making this suggested change breaks it. The whole link menu just disappears. Any ideas on fixing my 404? When you invoke your menu function, you still need to pass in the argument. E.g.: menu($_menu); Link to comment https://forums.phpfreaks.com/topic/204857-how-can-i-use-this-menu-function-to-link-to-external-urls/#findComment-1072439 Share on other sites More sharing options...
healthbasics Posted June 15, 2010 Author Share Posted June 15, 2010 When you invoke your menu function, you still need to pass in the argument. E.g.: menu($_menu); Thanks that fixed it. Do you know how I can fix my 404 problem? Link to comment https://forums.phpfreaks.com/topic/204857-how-can-i-use-this-menu-function-to-link-to-external-urls/#findComment-1072441 Share on other sites More sharing options...
healthbasics Posted June 15, 2010 Author Share Posted June 15, 2010 Regarding the external links it worked but it broke my menu item highlighting. Before I did this the current page button would be highlighted. Do you know how I can get this back? Link to comment https://forums.phpfreaks.com/topic/204857-how-can-i-use-this-menu-function-to-link-to-external-urls/#findComment-1072451 Share on other sites More sharing options...
healthbasics Posted June 15, 2010 Author Share Posted June 15, 2010 \ Link to comment https://forums.phpfreaks.com/topic/204857-how-can-i-use-this-menu-function-to-link-to-external-urls/#findComment-1072589 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.