I have a set of arrays I’m getting back from the database, I’m using a php foreach loop to go through them. If I’m returning 5 arrays [1, 2, 3, 4, 5], each one containing a (time, status, location) I want to style all even arrays with a certain div and each odd ones with a different div. How would I do this?
I’m assuming I would use the loop with modulo operator.
for ($i = 1; $i <= $count; $i++)
if ($i % 2 == 0) {
} else {
}
}
<?php foreach ($reservation as $seat) : ?>
If it is array 1, 3, 5.
<div style="font-size:20px; background-color: #red; float:left;">
<?php echo $seat['location']; ?>
<?php echo $seat['time']; ?>
<?php echo $seat['status']; ?> </div>
If it is array 2, 4
<div style="font-size:20px; background-color: #blue; float:right;">
<?php echo $seat['location']; ?>
<?php echo $seat['time']; ?>
<?php echo $seat['status']; ?> </div>
<?php endforeach ?>
You do not have to use a foreach loop to enumerate all elements of an array:
I’d recommend setting CSS-classes “odd” and “even” to the divs and then style them in a separate file. If you want, you can also use CSS3 pseudo classes
:nth-child(odd)and:nth-child(even).