Page 1 of 1

simple navigation, not so simple

Posted: Sat May 10, 2003 6:08 pm
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 !!

Posted: Sat May 10, 2003 6:17 pm
by Doolittle
try using $_REQUEST['c'] instead of just $c

Posted: Sat May 10, 2003 6:25 pm
by m3mn0n
I'd use switch().

Posted: Sat May 10, 2003 7:05 pm
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.

Posted: Sat May 10, 2003 7:24 pm
by McGruff
Also $_GET['c']

Posted: Sat May 10, 2003 9:29 pm
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. :)

Posted: Sun May 11, 2003 12:25 am
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!

Posted: Sun May 11, 2003 3:12 am
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 -

Posted: Sun May 11, 2003 12:59 pm
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.

Posted: Mon May 12, 2003 10:49 am
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