473,395 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

sending variable to include file

Hi, I'm pretty new to php but I've been working on a site and have a
problem I can't fix, though it's probably pretty simple.

Basically I have an index.php file that includes a header.php file. I
attempt to pass a variable to it as follows:
<?include("header.php?page=Home");?>

Then the header.php file contains this code:
<?php
$page = $_GET['page'];
?>
<html><head><title><?$page?></title>

I want $page to contain the string "Home" of course, but it ends up
being nothing but an empty string.

Thanks in advance for your help.

Matt

Aug 18 '06 #1
5 5813
"vetchling" <ve*******@gmail.comwrites:
Hi, I'm pretty new to php but I've been working on a site and have a
problem I can't fix, though it's probably pretty simple.

Basically I have an index.php file that includes a header.php file. I
attempt to pass a variable to it as follows:
<?include("header.php?page=Home");?>

Then the header.php file contains this code:
<?php
$page = $_GET['page'];
?>
<html><head><title><?$page?></title>

I want $page to contain the string "Home" of course, but it ends up
being nothing but an empty string.

Thanks in advance for your help.
You can't add ?args to an include; PHP doesn't evalutate that when
including files. (In fact, it usually never evaluates URL arguments:
that's the HTTP daemon's job.) It merely copies the contents of
header.php directly into your document (or parses equivilantly).

To do this, you need to do:

#In mypage.php you need to access $mypage_page directly.
mypage_page = "Home";
include ("mypage.php");
I've prefixed the variable $page to avoid naming conflicts.

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 18 '06 #2
In article <11**********************@i42g2000cwa.googlegroups .com>,
ve*******@gmail.com says...
Hi, I'm pretty new to php but I've been working on a site and have a
problem I can't fix, though it's probably pretty simple.

Basically I have an index.php file that includes a header.php file. I
attempt to pass a variable to it as follows:
<?include("header.php?page=Home");?>
Write your header (and footer if so warranted, example included) as a
function which returns the header text with the variable embedded in it,
and pass the variable to the function.

# header_footer_functions.php
<?php
function print_header($this_page) {
$text = '<html><head><title>'.$this_page.'</title></head>';
return $text;
}
function print_footer($author, $dept) {
$text = '<div>Author: .'$author.'<br />Dept: '.$dept.'</div></html>';
return $text;
}
?>

# home_page.php
<?php
include_once('header_footer_functions.php')
echo print_header('home');
?>
... rest of home page
<?php
echo print_footer('Slave Driver', 'Head Office');
?>

# some_other_page.php
<?php
include_once('header_footer_functions.php')
echo print_header('some other page');
?>
... rest of some other page
<?php
echo print_footer('Underpaid', 'Underlings');
?>

Geoff M
Aug 18 '06 #3
vetchling wrote:
Hi, I'm pretty new to php but I've been working on a site and have a
problem I can't fix, though it's probably pretty simple.

Basically I have an index.php file that includes a header.php file. I
attempt to pass a variable to it as follows:
<?include("header.php?page=Home");?>

Then the header.php file contains this code:
<?php
$page = $_GET['page'];
?>
<html><head><title><?$page?></title>

I want $page to contain the string "Home" of course, but it ends up
being nothing but an empty string.
[rant against using includes as functions]

Just wrap a function around the HTML then call it. Later on you'll find
that it gives you a lot of flexibility. So instead of a header.php and
footer.php, you have one file, interface.php:

<?php function printHeader($page) { ?>
<html><head><title><? echo htmlspecialchars($page); ?></title>
<? } ?>

<?php function printFooter() { ?>
</body></html>
<? } ?>

In the different pages, you'd include this file then call the functions
at the right places:

<?php

require_once("interface.php");

printHeader('Welcome');

/* ... */

printFooter();

?>

Aug 18 '06 #4
Dear Matt, your Script should work, you just forgot add 'echo' which
prints your variable ...

<?php
$page = $_GET['page'];
?>
<html><head><title><?php echo $page; ?></title>
vetchling wrote:
Hi, I'm pretty new to php but I've been working on a site and have a
problem I can't fix, though it's probably pretty simple.

Basically I have an index.php file that includes a header.php file. I
attempt to pass a variable to it as follows:
<?include("header.php?page=Home");?>

Then the header.php file contains this code:
<?php
$page = $_GET['page'];
?>
<html><head><title><?$page?></title>

I want $page to contain the string "Home" of course, but it ends up
being nothing but an empty string.

Thanks in advance for your help.

Matt

--
--------------------------------------------
m2m server software gmbh - http://www.m2m.at
Aug 18 '06 #5
vetchling wrote:
Hi, I'm pretty new to php but I've been working on a site and have a
problem I can't fix, though it's probably pretty simple.

Basically I have an index.php file that includes a header.php file. I
attempt to pass a variable to it as follows:
<?include("header.php?page=Home");?>

Then the header.php file contains this code:
<?php
$page = $_GET['page'];
?>
<html><head><title><?$page?></title>

I want $page to contain the string "Home" of course, but it ends up
being nothing but an empty string.

Thanks in advance for your help.

Matt

You don't need to pass a variable to an included file, it already has
access to all variables in the includING script. It becomes part of the
script that is including it.

$page = 'Home';
include 'header.php';

Now header.php will see that $page contains 'Home'.

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*****************************
Aug 18 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Frostillicus | last post by:
I'm trying to get an ASP to return a zip file to the remote browser from an Image (BLOB) field in SQL Server 2000 but Internet Explorer keeps saying: Cannot open C:\Documents and...
2
by: Ian Hubling | last post by:
Here's what I'm trying to do: I have a large number of pages that call a pop-up survey page. It is important to know what page the survey is referring to - so I am passing the name of the...
3
by: Mike | last post by:
I'm new to PHP - moving over from ASP. I have a number of include files, the first of which sets the value of a variable $loginmsg. I use that variable in a subsequent include file, but get a...
9
by: Sandy | last post by:
can mfc application, send text data to opened notepad file in desktop?(live transfer of data) . can anybody help
6
by: Anuradha | last post by:
Dear All How can i send mails using vb.net Thanx all
13
by: GGawaran | last post by:
First off, Hi everyone new to .asp and am trying to self teach myself. Im trying to figure out what exactly im doing wrong, or maybe what I think I can do, I really cant. The Idea....
1
by: Jerim | last post by:
I have a PHP script which simply calls another PHP script using include(). Let's call them website.php and website_include.php, with website.php simply including website_include.php. I can pass the...
6
by: j.woodcock | last post by:
is there a way of having a file that's name is a variable (eg dependant on the user name) act like a include. i know that you cant define the file for an include asp tag using a variable and that...
3
by: leonardodiserpierodavinci | last post by:
Hi. Sorry for what is perhaps a neophyte question: is it possible to pass a variable to a PHP script from inside another PHP piece of code? For instance, the file test.php (which of course...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.