Jump to content


Photo

Simple Function Error


  • Please log in to reply
2 replies to this topic

#1 ethoemmes

ethoemmes
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 September 2006 - 07:09 PM

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

<?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>


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 10 September 2006 - 07:14 PM

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
function CurrentPageSelector($page, $currentpage) {
		 					if ($page == $currentpage) { 
								 return "id='active'";
							}
		 }

or
function CurrentPageSelector($page) {
                global $currentpage;
		 					if ($page == $currentpage) { 
								 return "id='active'";
							}
		 }


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 ethoemmes

ethoemmes
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 September 2006 - 07:26 PM

Thanks for the quick reply. Worked perfectly!

E




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users