Author Topic: PHP variable not passed to include  (Read 3409 times)

0 Members and 1 Guest are viewing this topic.

Offline lpaternosterTopic starter

  • Irregular
  • Posts: 4
    • View Profile
PHP variable not passed to include
« on: March 07, 2008, 02:58:38 PM »
Hi - I'm an absolute PHP newbie and this is my first bit of code.

I'm trying to set up a navbar that displays the current link differently from the other links in the list.  In order to do this, I'm declaring a variable right at the top of my HTML document.  For example:

Code: [Select]
<?php 
$section 
'home'
?>

I then include the navbar mark up using:

Code: [Select]
<?php include ("inc/navbar.php"); ?>
This works.  However, the variable doesn't seem to have survived.  This is my navbar.php file:

Code: [Select]
<div id="navbar">

<?php
echo $section;
?>


<ul>

<?php if ($section == 'home') { ?>

<li class="current">Home</li>

<?php } else { ?>

<li><a href="index.php">Home</a></li>

<?php ?>

</ul>

</div>

The
Code: [Select]
echo command does not return anything at all, which suggests the navbar.php doesn't recognise it(?)

Am I doing something wrong here?

TIA,

Leon


Offline wildteen88

  • Guru
  • 'Insane!'
  • *
  • Posts: 12,021
  • Gender: Male
    • View Profile
Re: PHP variable not passed to include
« Reply #1 on: March 07, 2008, 03:01:33 PM »
Strange that should work. Is any errors displayed?

Offline lpaternosterTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Re: PHP variable not passed to include
« Reply #2 on: March 07, 2008, 03:07:53 PM »
No - the echo in navbar.php doesn't display anything either.

Thanks,

Leon

Offline wildteen88

  • Guru
  • 'Insane!'
  • *
  • Posts: 12,021
  • Gender: Male
    • View Profile
Re: PHP variable not passed to include
« Reply #3 on: March 07, 2008, 04:34:18 PM »
Need to see more code then, specifically the code that includes inc/navbar.php

Offline lpaternosterTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Re: PHP variable not passed to include
« Reply #4 on: March 08, 2008, 02:50:09 PM »
The index.php page looks like this:

Code: [Select]
<?php 
$section 
'home'
?>


<?php include ("http://leonpaternoster.com/inc/header.php"); ?>

<?php include ("http://leonpaternoster.com/inc/navbar.php"); ?>

<img src="images/russia.jpg" alt="Slightly short-sighted but eager designer at work" height="333" width="760" class="bigpic">

<div id="content">

<div id="left-col">

<h2>Simple, accessible web design</h2>

<p>I design web pages that make it easy for your <em>users</em> to find the information they need.  That means you make more <em>money</em> when I design your pages.</p>

<p>I focus on:</p>

<dl>

<dt>Usability</dt>

<dd>All my design is based on studies of user behaviour and user testing.  This means that your users will be able to locate the information they need as effieciently as possible, which means more business for you.</dd>

<dt>Accessibilty</dt>

<dd>I use layout, wording and typography to ensure that as many users as possible can access the information on your website.</dd>

<dt>Speed</dt>

<dd>I code websites as efficiently as possible, removing unnecessary content.  This means that your users will find information as quickly as possible.</dd>

</dl>

<p class="cta">Find out <a href="about.html">more</a>, <a href="contact.html">contact me</a> or see my <a href="prices.html">services and prices</a>.</p>

</div>

<?php include ("http://leonpaternoster.com/inc/right-col.php"); ?>

<?php include ("http://leonpaternoster.com/inc/footer.php"); ?>

The navbar.php file looks like this:

Code: [Select]
<div id="navbar">

<?php
echo $section;
?>


<ul>

<?php if ($section == 'home') { ?>

<li class="current">Home</li>

<?php } else { ?>

<li><a href="http://leonpaternoster.com/index.php">Home</a></li>

<?php ?>

<?php if ($section == 'about') { ?>

<li class="current">About</li>

<?php } else { ?>

<li><a href="http://leonpaternoster.com/about.php">About</a></li>

<?php ?>

<?php if ($section == 'contact') { ?>

<li class="current">Contact</li>

<?php } else { ?>

<li><a href="http://leonpaternoster.com/contact.php">Contact</a></li>

<?php ?>

<?php if ($section == 'prices') { ?>

<li class="current">Prices</li>

<?php } else { ?>

<li><a href="http://leonpaternoster.com/prices/index.php">Prices</a></li>

<?php ?>

<?php if ($section == 'examples') { ?>

<li class="current">Examples</li>

<?php } else { ?>

<li><a href="http://leonpaternoster.com/examples/index.php">Examples</a></li>

<?php ?>

</ul>

</div>

In my the-last-programming-I-did-was-on-an-Amiga way, could this be anything to do with scope (i.e. $section is local to the index.php page)?

Thanks for your help,

Leon

Offline lpaternosterTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Re: PHP variable not passed to include
« Reply #5 on: March 08, 2008, 02:55:42 PM »
If you want an idea of how I'd like it to look, see http://www.chrisgrafham.com/leon/index.html.

Offline wildteen88

  • Guru
  • 'Insane!'
  • *
  • Posts: 12,021
  • Gender: Male
    • View Profile
Re: PHP variable not passed to include
« Reply #6 on: March 09, 2008, 10:40:49 AM »
Because you are including files using the http:// protocol within the include function PHP will parse the code in those files separately and only return the output, it wont use variables which you set within the main script

Use relative paths instead, change:
Code: [Select]
<?php 
$section 
'home'
?>


<?php include ("http://leonpaternoster.com/inc/header.php"); ?>

<?php include ("http://leonpaternoster.com/inc/navbar.php"); ?>
to:
Code: [Select]
<?php 

$section 
'home'

define('ROOT'$_SERVER['DOCUMENT_ROOT']);

include 
ROOT'/inc/header.php';
include 
ROOT'/inc/navbar.php';
?>

You'll also need to change the last two includes to:
Code: [Select]
<?php
include ROOT'/inc/right-col.php';
include 
ROOT'/inc/footer.php';
?>
« Last Edit: March 09, 2008, 10:42:35 AM by wildteen88 »