Forum Moderators: coopster

Message Too Old, No Replies

Different php include based on URL variables is not working

Help, this is driving me crazy

         

brizad

6:59 pm on Jun 4, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



I don't really know php. I can usually figure it out and edit it based on stuff I find on the web. However this time I'm stumped. I've searched for a solution for 2 days and can't figure this out.

I'm trying to have different text included in a div based on a variable in the URL.

For instance, http://www.example.com/?formType=AIR-CAR-HOTEL
would include air-car-hotel.php

and

http://www.example.com/?formType=CAR-HOTEL
would include car-hotel.php

The problem is that only the last statement is being included:
} else {
include('include/air-only.php');

and none of the URL variables are making the include change as I want it to.

Here's the code I have:


<?php
if(isset($_GET['AIR-ONLY'])){
include('include/air-only.php');
}else if(isset($_GET['AIR-CAR'])){
include('include/air-car.php');
}else if(isset($_GET['AIR-CAR-HOTEL'])){
include('include/air-car-hotel.php');
}else if(isset($_GET['AIR-HOTEL'])){
include('include/air-hotel.php');
}else if(isset($_GET['CAR-HOTEL'])){
include('include/car-hotel.php');
} else {
include('include/air-only.php');
}
?>

Can anyone tell me what I'm doing wrong here? I really appreciate it!

[edited by: dreamcatcher at 6:47 am (utc) on June 5, 2008]
[edit reason] use example.com. Thanks. [/edit]

eelixduppy

7:09 pm on Jun 4, 2008 (gmt 0)



You are trying to access different variables when there is only one variable with different possible values. I would do something like this:

if(!isset($_GET['formType'])) [url=http://www.php.net/exit]exit[/url];
#
switch($_GET['formType']) {
case 'AIR-ONLY':
$page = 'air-only.php';
break;
case 'AIR-CAR':
$page = 'air-car.php';
break;
# etc...
default:
$page = 'air-only.php';
}
include('include/$page');

This uses the switch statement [us2.php.net] here.

brizad

8:02 pm on Jun 4, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks eelixduppy.

I tried that code and it's giving an error.

Warning: include(include/$page) [function.include]: failed to open stream: No such file or directory in /home/#*$!#*$!/public_html/index2.html on line 244

Warning: include() [function.include]: Failed opening 'include/$page' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/#*$!#*$!/public_html/index2.html on line 244

I actually commented out that first line because it leaves a blank page if there's no variable. Instead I want it to use the default air-only.php.

Here's the exact code I have on my page. Do you see any thing wrong?

<?php
#if(!isset($_GET['formType'])) exit;
#
switch($_GET['formType']) {
case 'AIR-ONLY':
$page = 'air-only.php';
break;
case 'AIR-CAR':
$page = 'air-car.php';
break;
case 'AIR-CAR-HOTEL':
$page = 'air-car-hotel.php';
break;
case 'AIR-HOTEL':
$page = 'air-hotel.php';
break;
case 'CAR-HOTEL':
$page = 'car-hotel.php';
break;
default:
$page = 'air-only.php';
}
include('include/$page');
?>

Thanks.

Receptional Andy

8:05 pm on Jun 4, 2008 (gmt 0)



The problem is the single quotes in the include statement. Should be:

include("include/$page");

brizad

9:01 pm on Jun 4, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



The problem is the single quotes in the include statement. Should be:

include("include/$page");

That was it. Thanks a ton!

Receptional Andy

9:08 pm on Jun 4, 2008 (gmt 0)



The gory details are in the PHP manual entry for 'string' [php.net], which is always worth a read :)

eelixduppy

9:57 pm on Jun 4, 2008 (gmt 0)



oops, my fault; sorry about that :)

brizad

10:07 pm on Jun 4, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks to the both of you for your help!