Help with looping through an array
Posted: Mon Apr 07, 2008 3:46 pm
Hi,
I am trying to write a dynamic menu function to return the active page.
It works as required but I have added a sub menu and now the code will not pick up when one of the sub menu pages is active. I thought this could be achieved by assigning all sub menu pages to an array and testing whether the sub menu page was within that array.
Basically in the $tea_school array the active page should be tea_school.php when any of "tea_growing_processing.php", "tea_history.php", "tea_perfect_brew.php", "tea_faqs.php" are active.
Am I going about this in the right way? And can someone help me with the syntax?
MTIA
I am trying to write a dynamic menu function to return the active page.
It works as required but I have added a sub menu and now the code will not pick up when one of the sub menu pages is active. I thought this could be achieved by assigning all sub menu pages to an array and testing whether the sub menu page was within that array.
Basically in the $tea_school array the active page should be tea_school.php when any of "tea_growing_processing.php", "tea_history.php", "tea_perfect_brew.php", "tea_faqs.php" are active.
Am I going about this in the right way? And can someone help me with the syntax?
MTIA
Code: Select all
<?php
$currentpage = ltrim(($_SERVER['PHP_SELF']), "");
$tea_school = array();
$our_story = array();
$tea_school = array("tea_school.php", "tea_growing_processing.php", "tea_history.php", "tea_perfect_brew.php", "tea_faqs.php");
$our_story = array("our_story.php", "our_tea.php","our_future.php");
function CurrentPageSelector($page) {
global $currentpage;
if ($page == $currentpage) {
return "class=\"active\"";
}
foreach($tea_school as &value)
{if ($page == $value) {
return "class=\"active\"";
}
}
}
}
?>
<ul id="navbar">
<li>
<a href="index.php" <?php echo CurrentPageSelector("/index.php");?>>Home</a>
</li>
<li>
<a href="/shop/index.php" <?php echo CurrentPageSelector("/shop/index.php");?>>Tea Shop</a>
</li>
<li>
<a href="our_story.php" <?php echo CurrentPageSelector("/our_story.php");?>>Our Story</a>
</li>
<li>
<a href="fine_teas.php" <?php echo CurrentPageSelector("/fine_teas.php");?>>Fine Teas</a>
</li>
<li>
<a href="tea_school.php" <?php echo CurrentPageSelector("/tea_school.php");?>>Tea School</a>
</li>
<li>
<a href="tea_benefits.php" <?php echo CurrentPageSelector("/tea_benefits.php");?>>Benefits</a>
</li>
<li>
<a href="talk_to_us.php" <?php echo CurrentPageSelector("/talk_to_us.php");?>>Talk to Us</a>
</li>
</ul>