How to add different class to even and odd div

02nd, January 2018 By

To assign different class to div based on its even and odd place you have to use the following code and follow these steps.

Step1:

Define different classes for odd and even divs for example I have used here evenclass and oddclass.

<style>
 .evenclass
 {
 background-color:yellow;
 }
 .oddclass
 {
 background-color:red;
 }
</style>

Step 2:

In php use particular array with loop to repeat the records in the loop for each record use a variable and increase it by one on every repeation and find remainder on every record if divided by 2 gives zero then assign it even class other wise assign it odd class.


<?php

<em id="__mceDel">$resultsAll = array();
$resultsAll[] = 1;
$resultsAll[] = 2;
$resultsAll[] = 3;
$autoincrement = 0;
foreach ($resultsAll as $record) {
 ?>
 <div<?php if ($autoincrement % 2 == 0) { ?> class="evenclass" <?php } else { ?> class="oddclass" <?php } ?>></em>

hello<?php echo $autoincrement; ?>

<td align="center" ></td>

</div>

<?php $autoincrement++;
} ?>

Here you can see the output:

 

Odd Even

Leave a Reply

Your email address will not be published. Required fields are marked *