//set the array
$info = array(
'andy' => 'blue',
'andrew' => 'black',
'mark' => 'green',
'jane' => 'orange',
'simon' => 'red',
'joan' => 'pink',
'sue' => 'yellow',
'alan' => 'black')
$i = 1;
foreach($info as $key => $val){
<div class="holder">
<div class="name">
<?php echo $Name ?>
</div>
<div class="colour">
<?php echo $colour ?>
</div>
</div>
}
This dispalys each "holder" class... but what I am wanting to do is wrap a container around the "holder" class and have 3 "holder" in each "container". eg:
<div class="container">
<div class="holder">
<div class="name">
<?php echo $Name ?>
</div>
<div class="colour">
<?php echo $colour ?>
</div>
</div>
<div class="holder">
<div class="name">
<?php echo $Name ?>
</div>
<div class="colour">
<?php echo $colour ?>
</div>
</div>
<div class="holder">
<div class="name">
<?php echo $Name ?>
</div>
<div class="colour">
<?php echo $colour ?>
</div>
</div>
</div>
I cant find out how to either get the index of the associative array, or how to break a foreach loop once %3 == 0.
Any suggestions would be awesome!
-Ved
You need to have a separate counter variable:
$i = 0;
foreach($info as $key => $val){
if($i%3 == 0) {
echo $i > 0 ? "</div>" : ""; // close div if it's not the first
echo "<div class='container'>";
}
?>
<div class="holder">
<div class="name">
<?php echo $Name ?>
</div>
<div class="colour">
<?php echo $colour ?>
</div>
</div>
<?php
$i++;
}
?>
</div> <!-- close last container div -->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With