Jump to content

How can I use this menu function to link to external URL's?


healthbasics

Recommended Posts

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

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

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

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

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

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

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