simple navigation, not so simple

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Maxaipa
Forum Newbie
Posts: 17
Joined: Sat May 10, 2003 6:08 pm

simple navigation, not so simple

Post by Maxaipa »

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 !!
Doolittle
Forum Newbie
Posts: 19
Joined: Sun May 04, 2003 11:45 pm

Post by Doolittle »

try using $_REQUEST['c'] instead of just $c
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

I'd use switch().
Maxaipa
Forum Newbie
Posts: 17
Joined: Sat May 10, 2003 6:08 pm

Post by Maxaipa »

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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Also $_GET['c']
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Sure.

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;
?>
More info on it at the link in the first post. :)
Maxaipa
Forum Newbie
Posts: 17
Joined: Sat May 10, 2003 6:08 pm

Post by Maxaipa »

Excellent information Sami... I really appreciate your time. Looks like I got a little reading to do and I'm sure I'll be checking in to this forum on a regular basis.

Cheers!
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

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

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';
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 -
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Yea that's a flaw, but it can be worked bypassed by using only the $_POST method.


Hopefully it will be fixed in a later version of php.
Maxaipa
Forum Newbie
Posts: 17
Joined: Sat May 10, 2003 6:08 pm

Post by Maxaipa »

Gentlemen... thanks a bunch for the advice... I've tried your suugestions and am now at least getting things to work. That's leaps and bounds ahead of where I was just a day or two ago. I'll be keeping an eye out for other posts with your names on them.

Hip, hip and tally-ho...

Maxaipa
Post Reply