Jump to content

PHP variable not passed to include


lpaternoster
 Share

Recommended Posts

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:

 

<?php 
$section = 'home'; 
?>

 

I then include the navbar mark up using:

 

<?php include ("inc/navbar.php"); ?>

 

This works.  However, the variable doesn't seem to have survived.  This is my navbar.php file:

 

<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

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

 

 

Link to comment
Share on other sites

The index.php page looks like this:

 

<?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:

 

<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

Link to comment
Share on other sites

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:

<?php 
$section = 'home'; 
?>

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

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

to:

<?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:

<?php
include ROOT. '/inc/right-col.php';
include ROOT. '/inc/footer.php';
?>

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.