Reputation: 18123
So what I wish to accomplish is:
<div class="product-box">
<ul class="product-list">
<li><a href="http://sauritchsurfboards.com/longboards.html" title="Surfboards _ San Diego Surfboard Shapers _ Sauritch Surfboards-2" target="_blank"><span>Surfboards _ San...</span>
</a></li>
<li><a href="http://sauritchsurfboards.com/longboards.html" title="Surfboards _ San Diego Surfboard Shapers _ Sauritch Surfboards-2" target="_blank"><span>Surfboards _ San...</span></a></li>
<li><a href="http://sauritchsurfboards.com/longboards.html" title="Surfboards _ San Diego Surfboard Shapers _ Sauritch Surfboards-2" target="_blank"><span>Surfboards _ San...</span></a></li>
<li><a href="http://sauritchsurfboards.com/longboards.html" title="Surfboards _ San Diego Surfboard Shapers _ Sauritch Surfboards-2" target="_blank"><span>Surfboards _ San...</span></a></li>
<li><a href="http://sauritchsurfboards.com/longboards.html" title="Surfboards _ San Diego Surfboard Shapers _ Sauritch Surfboards-2" target="_blank"><span>Surfboards _ San...</span></a></li>
<li><a href="http://sauritchsurfboards.com/longboards.html" title="Surfboards _ San Diego Surfboard Shapers _ Sauritch Surfboards-2" target="_blank"><span>Surfboards _ San...</span></a></li>
</ul>
</div>
I have a $griditems
which is a array from the database.
I would like to show 6 .product-box'es with 6 <li>
's inside that is from this $griditems
Normally I would just have done:
<div class="product-box">
<ul class="product-list">
<?php
$count=1;
foreach($griditems as $item)
{
if($count == 6) { break; }
?>
<li><a>.....</a></li>
<?php
$count++;
}
?>
</ul>
</div>
But this will not work in this case, since I would like to have 6 of these .product-box classes, and inside each .product-box should be these 6 items, which should continue from eachother and not repeating the same first 6 that exists in the $griditems
array.
How can I do this?
Upvotes: 0
Views: 121
Reputation: 7050
I may have misunderstood your question, but I think this will help:
$count = 0;
echo "<ul>"
foreach($griditems as $item) {
if (++$count % 6 == 0) {
echo "</ul><ul>";
}
echo "<li><a>....</a></li>";
}
echo "</ul>";
Upvotes: 0
Reputation: 34628
You can use array_chunk
to split the array into sub-arrays of a certain maximum length.
$chunks = array_chunk($griditems, 6);
foreach($chunks as $chunk) {
echo "<div><ul>";
foreach($chunk as $item) echo "<li>".$item."</li>";
echo "</ul></div>";
}
Incidentally, it's not great practice to use an HTML element merely to house a single, other HTML element, so your DIV may be superflous.
Upvotes: 1
Reputation: 56779
You want to use the mod operator (%
) to check for every sixth item, and then close the existing box and start a new one, something like this:
?>
<li><a>.....</a></li>
<?php
if ($count % 6 == 0) { ?>
</ul>
</div>
<div class="product-box">
<ul class="product-list">
<?php }
...
Upvotes: 0