lpaternoster Posted March 7, 2008 Share Posted March 7, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/94946-php-variable-not-passed-to-include/ Share on other sites More sharing options...
wildteen88 Posted March 7, 2008 Share Posted March 7, 2008 Strange that should work. Is any errors displayed? Quote Link to comment https://forums.phpfreaks.com/topic/94946-php-variable-not-passed-to-include/#findComment-486362 Share on other sites More sharing options...
lpaternoster Posted March 7, 2008 Author Share Posted March 7, 2008 No - the echo in navbar.php doesn't display anything either. Thanks, Leon Quote Link to comment https://forums.phpfreaks.com/topic/94946-php-variable-not-passed-to-include/#findComment-486369 Share on other sites More sharing options...
wildteen88 Posted March 7, 2008 Share Posted March 7, 2008 Need to see more code then, specifically the code that includes inc/navbar.php Quote Link to comment https://forums.phpfreaks.com/topic/94946-php-variable-not-passed-to-include/#findComment-486435 Share on other sites More sharing options...
lpaternoster Posted March 8, 2008 Author Share Posted March 8, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/94946-php-variable-not-passed-to-include/#findComment-487152 Share on other sites More sharing options...
lpaternoster Posted March 8, 2008 Author Share Posted March 8, 2008 If you want an idea of how I'd like it to look, see http://www.chrisgrafham.com/leon/index.html. Quote Link to comment https://forums.phpfreaks.com/topic/94946-php-variable-not-passed-to-include/#findComment-487157 Share on other sites More sharing options...
wildteen88 Posted March 9, 2008 Share Posted March 9, 2008 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'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/94946-php-variable-not-passed-to-include/#findComment-487752 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.