bronzemonkey Posted June 8, 2007 Share Posted June 8, 2007 Every page on my site contains a variable - bodyID - with one of 7 values. Each pages also requires the "header.php" file which contains script that acts upon the bodyID variable to affect my menu. The php in header.php that relies on the bodyID variable in every other page, looks like this: <a<?php if ( $bodyID == 'b1' ) { echo ' id="active"';} ?> href="#no">Link</a> My server error log is throwing up 7 PHP notices (one for each time the above script occurs) every time a page is accessed - Undefined variable: bodyID in /public_html/header.php My question is: how can I overcome these PHP Notices so that the variable is considered as defined...or do I even need to? thanks Quote Link to comment Share on other sites More sharing options...
Caesar Posted June 8, 2007 Share Posted June 8, 2007 You can: A) Define your variables and not asume register_globals are enabled. B) Change the error_reporting level on the server so that it only reports errors...and not notices error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted June 8, 2007 Share Posted June 8, 2007 how is it defined at present? you can always set a default value before hand... <?php $bodyID = isset($bodyID) ? $bodyID : 'b1'; switch($bodyID) { case 'b1': $aid = 'active'; break; case 'b2': $aid = 'working'; break; case 'b3': $aid = 'lazy'; break; case 'b4': $aid = 'sleeping'; break; case 'b5': $aid = 'running'; break; case 'b6': $aid = 'falling'; break; case 'b7': $aid = 'screaming'; break; default: $adi = 'finished'; } ?> <a id="<?php echo $aid; ?>" href="#no">Link</a> that is how I woudl approch it anyway... Quote Link to comment Share on other sites More sharing options...
bronzemonkey Posted June 8, 2007 Author Share Posted June 8, 2007 Thanks for the suggestions. I think this one looks the most suitable for me to use the in "header.php" (I don't own the server and changing its settings seems off limits) <?php $bodyID == isset($bodyID) ? $bodyID : ''; switch($bodyID) { case 'b1': $aid = ' id="active"'; break; case 'b2': $aid = ' id="active"'; break; case 'b3': $aid = ' id="active"'; break; case 'b4': $aid = ' id="active"'; break; case 'b5': $aid = ' id="active"'; break; case 'b6': $aid = ' id="active"'; break; case 'b7': $aid = ' id="active"'; break; default: $aid = ''; } ?> However, I need to write ' id="active"' into only 1 of 7 positions depending on if that position corresponds to the $bodyID value found in a page. This is what I am using at the moment, and it works, but it bringing up thousands of PHP Notices in the log and making it hard to find actual errors etc: <a<?php if ( $bodyID == 'b1' ) { echo ' id="active"';} ?> href="#no">Link</a> <a<?php if ( $bodyID == 'b2' ) { echo ' id="active"';} ?> href="#no">Link</a> <a<?php if ( $bodyID == 'b3' ) { echo ' id="active"';} ?> href="#no">Link</a> <a<?php if ( $bodyID == 'b4' ) { echo ' id="active"';} ?> href="#no">Link</a> <a<?php if ( $bodyID == 'b5' ) { echo ' id="active"';} ?> href="#no">Link</a> <a<?php if ( $bodyID == 'b6' ) { echo ' id="active"';} ?> href="#no">Link</a> <a<?php if ( $bodyID == 'b7' ) { echo ' id="active"';} ?> href="#no">Link</a> I'm a novice and so I could not work out how to use something like switch to do this for me, or how to adapt your suggestion (including the default definition - which would be used only for the index/sitemap pages) to replicate this effect. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted June 8, 2007 Share Posted June 8, 2007 mate I just did it for you!!!! Quote Link to comment Share on other sites More sharing options...
bronzemonkey Posted June 8, 2007 Author Share Posted June 8, 2007 Here is a clarified and complete version of my problem: Every page on my website has this code at the top: <? $title = "My website title | page title" ?> <? $bodyID = 'b1'; ?> <?php require("header.php"); ?> The $bodyID value can be one of b1, b2, b3, b4, b5, b6, b7. I use one of these 7 bodyID values every time I make a new page, in order to signify what category it is part of in my menu. These values are then used by my header.php file <html> <head> <title><? if(isset($title)) echo $title; else echo "My website title"; ?> </title> </head> <body<? if(isset($bodyID)) echo ' id="'.$bodyID.'"'; ?>> <div id="menu"> <ul> <li><a<?php if ( $bodyID == 'b1' ) { echo ' id="active"';} ?> href="#no">Cats</a> <ul> <li><a href="#" title="">Black cats</a></li> <li><a href="#" title="">White cats</a></li> <li><a href="#" title="">Purple cats</a></li> <li><a href="#" title="">Green cats</a></li> <li><a href="#" title="">No cats</a></li> </ul> </li> <li><a<?php if ( $bodyID == 'b2' ) { echo ' id="active"';} ?> href="#no">Dogs</a> <ul> <li><a href="#" title="">Big dogs</a></li> <li><a href="#" title="">Small dogs</a></li> <li><a href="#" title="">Round dogs</a></li> <li><a href="#" title="">Square dogs</a></li> <li><a href="#" title="">Triangular dogs</a></li> </ul> </li> <li><a<?php if ( $bodyID == 'b3' ) { echo ' id="active"';} ?> href="#no">Frogs</a> <ul> <li><a href="#" title="">Flying frogs</a></li> <li><a href="#" title="">Flat frogs</a></li> <li><a href="#" title="">Sharp frogs</a></li> <li><a href="#" title="">Toad frogs</a></li> </ul> </li> <li><a<?php if ( $bodyID == 'b4' ) { echo ' id="active"';} ?> href="#no">Mice</a> <ul> <li><a href="#" title="">One blind mouse</a></li> <li><a href="#" title="">Another blind mouse</a></li> <li><a href="#" title="">The last blind mouse</a></li> </ul> </li> <li><a<?php if ( $bodyID == 'b5' ) { echo ' id="active"';} ?> href="#no">Rats</a> <ul> <li><a href="#" title="">You dirty rat</a></li> <li><a href="#" title="">Bag o rats</a></li> <li><a href="#" title="">Pet rats</a></li> </ul> </li> <li><a<?php if ( $bodyID == 'b6' ) { echo ' id="active"';} ?> href="#no">Bats</a> <ul> <li><a href="#" title="">Blind as a mouse</a></li> <li><a href="#" title="">Bats and balls</a></li> </ul> </li> <li><a<?php if ( $bodyID == 'b7' ) { echo ' id="active"';} ?> href="#no">Fleas</a> <ul> <li><a href="#" title="">Circus</a></li> <li><a href="#" title="">Flea the law</a></li> <li><a href="#" title="">Flease</a></li> </ul> </li> </ul> </div> Any page about cats has $bodyID=b1, any about dogs has b2, etc. So that the xhtml generated for a page with $bodyID=b1 looks like this: <html> <head> <title>My website title | Cats</title> </head> <body id="b1"> <div id="menu"> <ul> <li><a id="active" href="#no">Cats</a> <ul> <li><a href="#" title="">Black cats</a></li> <li><a href="#" title="">White cats</a></li> <li><a href="#" title="">Purple cats</a></li> <li><a href="#" title="">Green cats</a></li> <li><a href="#" title="">No cats</a></li> </ul> </li> <li><a href="#no">Dogs</a> <ul> <li><a href="#" title="">Big dogs</a></li> <li><a href="#" title="">Small dogs</a></li> <li><a href="#" title="">Round dogs</a></li> <li><a href="#" title="">Square dogs</a></li> <li><a href="#" title="">Triangular dogs</a></li> </ul> </li> <li><a href="#no">Frogs</a> <ul> <li><a href="#" title="">Flying frogs</a></li> <li><a href="#" title="">Flat frogs</a></li> <li><a href="#" title="">Sharp frogs</a></li> <li><a href="#" title="">Toad frogs</a></li> </ul> </li> <li><a href="#no">Mice</a> <ul> <li><a href="#" title="">One blind mouse</a></li> <li><a href="#" title="">Another blind mouse</a></li> <li><a href="#" title="">The last blind mouse</a></li> </ul> </li> <li><a href="#no">Rats</a> <ul> <li><a href="#" title="">You dirty rat</a></li> <li><a href="#" title="">Bag o rats</a></li> <li><a href="#" title="">Pet rats</a></li> </ul> </li> <li><a href="#no">Bats</a> <ul> <li><a href="#" title="">Blind as a mouse</a></li> <li><a href="#" title="">Bats and balls</a></li> </ul> </li> <li><a href="#no">Fleas</a> <ul> <li><a href="#" title="">Circus</a></li> <li><a href="#" title="">Flea the law</a></li> <li><a href="#" title="">Flease</a></li> </ul> </li> </ul> </div> If the bodyID value were b2 then the "dogs" <a> element would have id="active" while "cats" would go back to having no id, and so on. The problem is that this is throwing up a PHP Notice undefined variable bodyID for each of the 7 pieces of php contained within the <div id="menu"></div> markup every time a page is accessed...it makes reading my error logs extremely laborious. Could someone please help me find a solution Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted June 8, 2007 Share Posted June 8, 2007 Does your PHP setup have the short_open_tag directive enabled in the php.ini? As I se you use both <? and <?php tags. <? is used in the file that requires header.php What happens if you change this: <? $title = "My website title | page title" ?> <? $bodyID = 'b1'; ?> <?php require("header.php"); ?> to this: <?php $title = "My website title | page title" ?> <?php $bodyID = 'b1'; ?> <?php require("header.php"); ?> Does the error go away? Quote Link to comment Share on other sites More sharing options...
bronzemonkey Posted June 8, 2007 Author Share Posted June 8, 2007 Server lets me use <? and <?php tags. The php notice for undefined variable occurs even on pages that only contain <?php require("header.php"); ?> Quote Link to comment Share on other sites More sharing options...
bronzemonkey Posted June 8, 2007 Author Share Posted June 8, 2007 Anyone got any other suggestions? Quote Link to comment Share on other sites More sharing options...
bronzemonkey Posted June 9, 2007 Author Share Posted June 9, 2007 bump Quote Link to comment Share on other sites More sharing options...
bronzemonkey Posted June 9, 2007 Author Share Posted June 9, 2007 I think I've solved this problem by modifying a suggestion to a similar problem on another forum. I changed the code to: <?php if(isset($bodyID) && $bodyID == 'value' ) { echo ' id="active"';} ?> Quote Link to comment 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.