[tutorial] strip table with PHP


Table comes in various styles, and the striped style is the most popular among them, normally when you make the table with pure HTML’s tags you could write the style down with CSS for each row. The idea is how to make the row’s background change dynamically according to the row. the odd rows has a different background color with the even one. Because we gonna make it from PHP code.
to make table above, first we have to define the color you’ll use. for example you will make a gray-white strip. so for gray we will use #EEEEEE color’s code and for white one, we just need to write ‘white’ or you can use code ‘#FFFFFF’. As we both know that table has both odd and even rows, so the plan is to make the odd rows has white background and the even one has the gray background. well, how do we know which one is even and which one is odd???? good question because we all know that PHP has modulus arithmetic operator ‘%’ .
the next step is make the code…. yaaay…

<table>
 <?php
 for ($i=1;$i&lt;=10;$i++)
 {
 Echo “
 <tr bgcolor=".($i%2?'#EEEEEE':'white').">"; ?>  
 <td bgcolor="">Isi 1 </td>
 <td bgcolor="">Isi 2</td>
 <td bgcolor="">Isi 3</td>
 <td bgcolor="">Isi 4</td>
 <td bgcolor="">Isi 5</td>
 </tr>
<?php } ?>
 </table>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s