ethoemmes Posted September 10, 2006 Share Posted September 10, 2006 Hi,I am trying to write a function (my first one) to help select the current page within my menu include.Basically the function is called from each menu <li>. If the <li> is the current page the function should ouput id="active". Can anyone spot what I am doing wrong?Thanks[code]<?php $currentpage = ltrim(($_SERVER['PHP_SELF']), "/dev/"); function CurrentPageSelector($page) { if ($page == $currentpage) { return "id='active'"; } }?> <div id="navcontainer"> <ul id="navlist"> <li <?php echo CurrentPageSelector("index.php");?>> <a href="index.php" id=>Introduction</a> </li> <li <?php echo CurrentPageSelector(catalogue.php);?>> <a href="catalogue.php">Catalogue 2</a> </li> <li <?php echo CurrentPageSelector("back_catalogue.php");?>> <a href="back_catalogue.php">Back Catalogues</a> </li> <li <?php echo CurrentPageSelector("mailinglist.php");?>> <a href="mailinglist.php">Mailing List</a> </li> <li <?php echo CurrentPageSelector("contact.php");?>> <a href="contact.php">Contact Form</a> </li> <li> <?php echo "page: " . $page . "current: " .$currentpage ?> </li> </ul> </div> <br /> <ul id="leftbar"> <h2 id="leftbartitle">Resource Area</h2> <li> <a href="#">Portrait Gallery</a> </li> <li> <a href="#">Title Pages</a> </li> <li> <a href="#">Bibliography</a> </li> </ul> </div> [/code] Link to comment https://forums.phpfreaks.com/topic/20318-simple-function-error/ Share on other sites More sharing options...
Barand Posted September 10, 2006 Share Posted September 10, 2006 Variables inside a function a function are local to that function, so $currentpage in the function is not the $currentpage variable outside the function.Either pass $currentpage as a second argument to the functionOr, if you really must, define $currentpage as global in side the function so it know to use the one already definedSo [code]function CurrentPageSelector($page, $currentpage) { if ($page == $currentpage) { return "id='active'"; } }[/code]or [code]function CurrentPageSelector($page) { global $currentpage; if ($page == $currentpage) { return "id='active'"; } }[/code] Link to comment https://forums.phpfreaks.com/topic/20318-simple-function-error/#findComment-89479 Share on other sites More sharing options...
ethoemmes Posted September 10, 2006 Author Share Posted September 10, 2006 Thanks for the quick reply. Worked perfectly!E Link to comment https://forums.phpfreaks.com/topic/20318-simple-function-error/#findComment-89484 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.