Conditions inside foreach loop

Hello,

I’m making dynamic nav bar and I have small problem. I want to count to 6 then add dropdown to nav menu, I write this code and its not working

foreach($nav as $items) {
    static $i = 0;
    echo "<li><a href=\"category.php?cat={$items}\">{$items}</a></li>";
    if ($i === 6) {
        echo "
            <li class=\"dropdown-nav\"><a href=\"javascript: void(0)\">Više <i class=\"fa fa-caret-down\"></i></a>
               <ul class=\"dropdown-content-nav\">
                 <li><a href=\"category.php?cat={$items}\">{$items}</a></li>
               </ul>
            </li>";
    }
}

This is never going to be true. You don’t appear to increment $i anywhere. I don’t see any reason why you need to use the static keyword when initialising $i.

3 Likes

I forgot it to add here (increment)
When I use $i without static $i value is always 0.

It is always 0 because it is not incremented, it stays the same.

1 Like
foreach($nav as $items) {
          static $i = 0;
          if ($i >= 6) {
            echo "
            <li class=\"dropdown-nav\"><a href=\"javascript: void(0)\">More <i class=\"fa fa-caret-down\"></i></a>
              <ul class=\"dropdown-content-nav\">
              <li><a href=\"\">{$items}</a></li>
              </ul>
          </li>";
          } else {
            echo "<li><a href=\"category.php?cat={$items}\">{$items}</a></li>";
          }
          $i++;
        }

Now i got three dropdowns, I have total 9 nav items and I want last 3 to be inside dropdown.
If I remove static keyword $i is 0.

How I can achive this?

Regards.

You should define $i before the loop.

3 Likes

Try this:


foreach($nav as $i => $items) 

No need to declare a static variable or the increment.

4 Likes

So long as the array has default numeric indexing.

1 Like

I am unable to test at the moment…

I believe $i starts counting from zero, in increments of one regardless of the array contents.

Thanks thats working.
Now I have a problem that loop creates three dropdowns with one item. How I can achive to create one dropdown with three items.

NOTE: My array contains 9 items and I want to output first 6 as normal li element and remaining 3 inside dropdown.

My code:

foreach($nav as $k => $items) {
    if ($k >= 6) {
        echo "
        <li class=\"dropdown-nav\"><a href=\"javascript: void(0)\">More <i class=\"fa fa-caret-down\"></i></a>
          <ul class=\"dropdown-content-nav\">
            <li><a href=\"category.php?cat={$items}\">{$items}</a></li>
          </ul>
        </li>";
    } else {
      echo "<li><a href=\"category.php?cat={$items}\">{$items}</a></li>";
    }
}
1 Like

That’s because every time you encounter an item where $k is six or higher, you define the entire dropdown. What you need to do is only open the dropdown once, and only close it once. Something like:

$ddopen = false;
foreach($nav as $k => $items) {
    if ($k >= 6 && $ddopen == false ) { 
        echo "
        <li class=\"dropdown-nav\"><a href=\"javascript: void(0)\">More <i class=\"fa fa-caret-down\"></i></a>
          <ul class=\"dropdown-content-nav\">"; 
        $ddopen = true; 
     }
   echo "<li><a href=\"category.php?cat={$items}\">{$items}</a></li>";
}
// now close the dropdown, if it was opened
if ($ddopen == true) { 
  echo "</ul></li>";
  }
2 Likes

Thank you very much guys, I have learned new things today! :slight_smile:

Best regards!!

1 Like

A little OT maybe, but to clarify. If you had an associative array, with strings for keys, they would compare like default numeric values in the loop?

I don’t have anywhere to test at present.

1 Like

Whoops.

Yes you are right, I have had to declare a counter outside of a fornext loop in order to populate an id when using associative arrays to plot graphical svg points.

Many thanks once again for the correction.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.