The easiest way would be to use GET parameters (i.e data in the actual URL).
There are a number of ways you can structure a HTML link to get what you want. You can have something basic (but not very elegant looking), like
   <a href="index.php?page=Home">Home</a>

Then in your index.php code you'd probably have something like :

   <?php
   $page = urldecode(*$_GET['page']); *
   include_once header.inc; //Include a script that contains the
   general header information
   if(is_file(pages/*$page*.inc;)) //make sure they haven't requested a
   non-existant file
   {
       include_once pages/*$page*.inc; //Doesn't have to be .inc you
   could simply output HTML data if that's all your using.
   }
   else
   {
      include_once pages/Home.inc; //If the user wanted a file that
   doesn't exist, then just take them to the home page (or you could
   take them to an error page if that's what you want).
   }
   include_once footer.inc; //Which would contain the footer if you've
   got one.
   ?>


This is approximately how I do it although sometimes have a functions file I call, or a config file with basic presets, and various other things. I use .inc (for inclusion) as the file extension, so I can easily differentiate between .php files that customers will use (such as index.php, admin.php, login.php, etc..), and the included files.

In the header file I usually have something like that below.

----------- header.inc starts below this line ---------------

   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
   <html lang="en">
   <head>

   <title><?php echo *$page*; ?> - Insert Company or Website Name</title>

   <!-- Meta Tags -->
   <meta http-equiv="content-type" content="application/xhtml+xml;
   charset=utf-8">
   <meta name="robots" content="index, follow">

   <!-- CSS -->
   <link rel="stylesheet" href="css/main.css"
   media="screen,projection,tv,tty,handheld,embossed,braille,aural"
   type="text/css">
   <link rel="stylesheet" href="css/print.css" media="print"
   type="text/css">
   </head>
   <body>
   <div id="header">
           <h1>HEADER INFORMATION (or image) HERE</h1>
           <br />
       <!-- end header div -->
       </div>
   <div id="mainNav">
   <?php
   *$main_navigation_list* = array(0 => array( 'name' => 'Home',
'url' => 'index.php?page=Contact', 'title' => 'The home page'), 1 => array('name' =>
   'Contact' ,
                                                                 'url'
   => 'index.php?page=Contact',
'title' => 'The contact details')
                                               );
   ?>
           <ul id="main_nav_list">
           <?php
           foreach(*$main_navigation_list* as *$index* => *$nav_list*)
           {
               echo '<li><a href="' . *$nav_list*['url'] .'" title="' .
   *$nav_list*['title'] . '">'. *$nav_list*['name'] . '</a></li>';
           }
           ?>
           </ul>
       </div>
<!-- end mainNav div -->
       </div>

--------------- END header.inc ----------



In the header.inc I've manually created an array, then got PHP to go through the array to add the name, URL and other details from the array, but that's just to simplify this, usually I pull the navigation data from a database (or if there's no MySQL installed I might unserialise it from a file).

There are other ways of pulling the data. If you want nice URLs, you can have something like /pages/Home/ and then have a mod_rewrite rule in Apache to then change that into index.php?page=Home, (although you'll also need to change the <a href> to the new links. If you aren't running on apache, you can manually find the page information by doing print_r($_SERVER), and seeing what bits and pieces you can put together, but that's not nearly as good or reliable.

Sorry if there's too much info, but I'm guessing this is roughly what you'll be doing. If you want to have more than one variable (like say a sub page) then you add *&amp;* between each variable. E.g

   <a href="index.php?page=Home&amp;sub_page=more%20news">Home -
   Archived News</a>

You can usually get away with just using *&* as the separator but it probably won't validate properly if your making it in xhtml (as you should be). Also, if your not sure what the %20 means (a space) then look up urlencode <http://au.php.net/manual/en/function.urlencode.php> and urldecode <http://au.php.net/url_decode>.

Michael Kubler
*G*rey *P*hoenix *P*roductions <http://www.greyphoenix.biz>



Christopher W wrote:
At least I hope it is simple...

I am trying to get an HTML menu link to set a variable's value. For example, when a user clicks the "Home" button on my page it would cause $page = "home"; or clicking the "About Us" button will set $page="about_us"; etc.

I think this should be fairly simple but being completely new to php I just cannot seem to get it right.

Any help would be greatly appreciate.

Thank you in advance.

Reply via email to