Page 1 of 1

Alternating row colors tutorial

New postPosted: Sat Dec 28, 2013 4:23 pm
by Cat
Sometimes, when we work with tables, we probably want to change the color for every row (<td>).
There are many ways to do this, but I will describe the most simple ways.

So... let's say we have an array with 26 elements. But we don't really know that, so we do a `$count = count($myarray);`.
Good! Now we know how many elements the array has, so let's get to the point.

We want to print every element on a line, in a row. So ... we will do something like the following:


Example:
Code: [Select all] [Expand/Collapse] [Download] (Untitled)
<html>
<head><title>gee</title></head>
<body bgcolor="#FFFFFF">
<table width="100%">

<?php

// Let's say the array exists. as I said, it has 26 elements.
$array = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "26");

$count = count($array);
for($x=0;$x<=$count;$x++) {
        if(($x % 2) == 0) { $c = "#9999CC"; }
        else { $c = "#CCCCCC"; }
?>
<tr><td bgcolor="<?= $c ?>">
<?= $array[$x]; ?>
</td></tr>
<? } ?>
</table>
</body>
</html>


Demo!


Anyone who wishes to make additions or changes to this tutorial email them to webmaster@linuxguruz.com