I have a problem with my foreach which returns an extra empty element in output if the "if" condition matches the index count.
this is my code
<ul id="toplevel" class="menu">
<?php
foreach($categories as $category){
$parenturl = 'some url'.$category->category_id;
$parentname = $category->category_name;
?>
<li>
<?php echo link($parenturl, $parentname); ?>
<?php if($category->childs) : ?>
<ul>
<li>
<?php echo link($parenturl, $parentname); ?>
<ul>
<?php
$i=0;
foreach($category->childs as $child){
$i++;
$childurl = 'some url'.$child->category_id;
$childname = $child->category_name;
?>
<li><?php echo link($childurl, $childname); ?></li>
<?php
if($i % 10 == 0) :
?>
</ul></li><li><?php echo link($parenturl, $parentname); ?><ul>
<?php
endif;
}
?>
</ul>
</li>
</ul>
<?php endif; ?>
</li>
<?php
}
?>
</ul>
So if the $child has exactly 10 items than this will output 11 and I don't know why.
The Output looks like this:
<ul id="toplevel" class="menu">
<li>
<a>Main Category</a>
<ul>
<li>
<a>Main Category</a>
<ul>
<li><a>Sub 1</a></li>
<li><a>Sub 2</a></li>
<li><a>Sub 3</a></li>
<li><a>Sub 4</a></li>
<li><a>Sub 5</a></li>
<li><a>Sub 6</a></li>
<li><a>Sub 7</a></li>
<li><a>Sub 8</a></li>
<li><a>Sub 9</a></li>
<li><a>Sub 10</a></li>
</ul>
</li>
<li>
<a>Main Category</a>
<ul> </ul>
</li>
</ul>
</li>
</ul>
I was playing around with the count number but every time when the $child matches the number set it outputs one more. But if $child has less or more items than the count it works fine.
The HTML looks like this because I'm using jQuery Verical Megamenu by Design Chemical and that needs this layout