Jump to content

Simple Function Error


ethoemmes

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]
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.