mrlayance
mrlayance

Reputation: 663

PHP include problem, not sure what the problem is

<?php
$city = $_GET["city"];
$prov = $_GET["prov"];

$file = "citycountdetails.php?city=".$city."&prov=".$prov."";
?>

<?php include $file; ?>

Warning: include(citycountdetails.php?city=Halifax&prov=NS) [function.include]: failed to open stream: No such file or directory in ....

Not sure what I am doing wrong...

Thanks

Upvotes: 0

Views: 57

Answers (2)

Michael Berkowski
Michael Berkowski

Reputation: 270767

It is not possible to include a file with query string parameters, as PHP will interpret the query string as part of the filename. If you need to pass $city and $prov into citycountdetails.php, they are already available in the global scope.

Modify citycountdetails.php to reference $city and $prov as $GLOBALS['city'] and $GLOBALS['prov'] instead of $_GET['city'] $_GET['prov'].

// citycountdetails.php
<?php
$city = $GLOBALS['city'];
$prov = $GLOBALS['prov'];
?>

// Or similarly accomplished in functions.php
<?php 
function GetCity(){
   return $GLOBALS['city'];
}
function GetProvince(){
   return $GLOBALS['prov'];
}
?>

Upvotes: 0

Daryl Teo
Daryl Teo

Reputation: 5495

You cannot include a file with parameters in there I'm afraid!

Those parameters are only for http requests. php includes do not launch http requests, they just include local files from the file system.

There are several ways to achieve what you're trying to do:


access $_GET["city"] and $_GET["prov"] from within citycountdetails.php.

This is the quick ugly method,and leads to quite a bit of dependency, but its not bad at least.


declare $city and $prov, and access it from citycountdetails.php (using globals).

This is probably the worst way to do it, so don't do this. :D


create a functions file with methods to retrieve those parameters

Probably the easiest thing you can do now, without all the bad side effects.

functions.php

<?php 
function GetCity(){
   return $_GET['city'];
}
function GetProvince(){
   return $_GET['prov'];
}
?>

Your file

<?php
include 'functions.php';
include 'citycountdetails.php';
?>

city count details.php

<?php
$city = GetCity();
$prov = GetProvince();
?>

Upvotes: 2

Related Questions