Jump to content

Archived

This topic is now archived and is closed to further replies.

ethoemmes

Simple Function Error

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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 function

Or, if you really must, define $currentpage as global in side the function so it know to use the one already defined

So [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]

Share this post


Link to post
Share on other sites

×

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.