simple navigation, not so simple
Moderator: General Moderators
simple navigation, not so simple
I'm relatively new to PHP but have programmed in ASP and others in the past and this has me stumped. This is probably easy enough to answer, just not for me. Here's the problem: I have index.php - and within that page I have require(menu.php). The menu.php file is just a set of links like the following:
<a href="index.php?c=2">Page #2</a>
I also have this if/else statement in the index.php page:
if ($c != "") {
include("content".$c.".php");
} else {
include("content1.php");
}
I thought that this would work but apparently not. When I click on the links, I see the page appended with ?c=1, 2, etc. in the address and status bars, but the content of the page doesn't change. What am I missing here? It's PHP 4.3.1, btw. Any suggestions will be readily accepted.
Thanks !!
<a href="index.php?c=2">Page #2</a>
I also have this if/else statement in the index.php page:
if ($c != "") {
include("content".$c.".php");
} else {
include("content1.php");
}
I thought that this would work but apparently not. When I click on the links, I see the page appended with ?c=1, 2, etc. in the address and status bars, but the content of the page doesn't change. What am I missing here? It's PHP 4.3.1, btw. Any suggestions will be readily accepted.
Thanks !!
Alrighty then... that's the quickest response and subsequent fix I've had the pleasure of... I changed the variable as you suggested and la voila! It works! Thanks Doolittle.
Sami: would you mind showing me what a switch would look like in this particular case? I'm not that familiar with PHP just yet.
Thanks to both of you for your assistance.
Sami: would you mind showing me what a switch would look like in this particular case? I'm not that familiar with PHP just yet.
Thanks to both of you for your assistance.
Sure.
More info on it at the link in the first post. 
Code: Select all
<?php
switch ($c){
default:
require ("content1.php");
break;
case 1:
require ("content1.php");
break;
case 2:
require ("content2.php");
break;
case 3:
require ("content3.php");
break;
?>-
pootergeist
- Forum Contributor
- Posts: 273
- Joined: Thu Feb 27, 2003 7:22 am
- Location: UK
the problem with switch is dynamic extensibility - add another value for $c and you need to recode the switch with another value = more work.
I'd test the $var and check for a file - then include it or include a default
so if .php?c=this_page is passed to it it will look for contentthis_page.php - include it if found or output an error -
I'd test the $var and check for a file - then include it or include a default
Code: Select all
// assign $file_ref to either $c or 'default'
$file_ref = (isset($_REQUEST['c']) && $_REQUEST['c'] !== "") ? $_REQUEST['c'] : 'default';
// check if the specified include exists - include or echo error
(file_exists('content' .$file_ref. '.php')) ? include('content' .$file_ref. '.php') : echo 'page not found';