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] Quote Link to comment 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] Quote Link to comment 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 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.