Page 1 of 1

Help with looping through an array

Posted: Mon Apr 07, 2008 3:46 pm
by ethoemmes
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

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>
 

Re: Help with looping through an array

Posted: Mon Apr 07, 2008 3:59 pm
by *nixMe
looks like $tea_school should be declared as a global var in your function you wrote. Also, instead of iterating through the $tea_school and testing for a condition on each iteration try using in_array() php function.

Instead of this

Code: Select all

foreach($tea_school as &value){
if ($page == $value) {return "class=\"active\"";}
}
 
it would be

Code: Select all

if(in_array($page,$tea_school))
return "class=\"active\"";
 
This isn't wrong but I've always felt using php functions is efficient over php code equivalent.

Re: Help with looping through an array

Posted: Mon Apr 07, 2008 4:14 pm
by ethoemmes
Hi,

Thanks for the quick response and suggestions. I agree it makes more sense to use a php function that trying to write your own.

I have tried your suggestions as below but am getting an error
Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/cantonte/public_html/cantondev.co.uk/includes/menu.php on line 16
My code is as follows - I am guessing that the $currentpage is not a text string? Is there someway to convert?

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\"";
            }
                if(in_array($currentpage,$tea_school,false)){
                    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>
 

Re: Help with looping through an array

Posted: Mon Apr 07, 2008 4:18 pm
by psychotomus

Code: Select all

settype($currentstring,"string");

Re: Help with looping through an array

Posted: Mon Apr 07, 2008 4:37 pm
by *nixMe
Add $tea_school to your global declaration line.

Code: Select all

global $currentpage, $tea_school;
You are getting that error because your using a local variable that isn't an array (in this NULL because it doesn't exist). The above line should fix this.

Re: Help with looping through an array

Posted: Mon Apr 07, 2008 4:50 pm
by ethoemmes
Thanks so much for your reply. It is now working as required.

Cheers

E