communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "html and code" - download "alternate row colors" - open demo of "alternate row colors"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

html and code - alternate row colors was created/last modified on Sat 17 Apr 2004 10:32 PM

this will output a table with alternating row backround colors.
with this the user can read the entries much easier
       
<?php
       
     
// just some random data in an array
     
$data = array('this','is','just','an','example','to','show','the','alternating','row','colors');

     
// the two colors to switch between
     
$rowcolor1 = '#F0F0F2';
     
$rowcolor2 = '#FFFFFF';
     
// the background colors on mouseover
     
$hovercolor1 = '#BAD4EB';
     
$hovercolor2 = '#DCE9F4';
       
     echo
'
<table style="caption-side:top; border-collapse:collapse">
     <caption>Demonstration</caption>'
;
       
     for (
$n = 0; $n < count($data); $n++)
     {
         
// this is where the magic happens
         
if($n % 2 == 1)
         {
              
// add more things to swop with each cycle
              
$style = $rowcolor1;
              
$hoverstyle = $hovercolor1;
         }else{
              
$style = $rowcolor2;
              
$hoverstyle = $hovercolor2;
         }

         echo
'
     <tr id="row'
.$n.'" style="background:'.$style.';" onmouseover="this.style.background=\''.$hoverstyle.'\'" onmouseout="this.style.background=\''.$style.'\'">
         <td>'
.$data[$n].'</td>
     </tr>'
;

     }
       
     echo
'
</table>'
;
       
?>

learn more about some of the PHP functions used in this snippet: array, count, each, echo, else, for, ord, rand, round, switch

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

PHParadise.com - PHP-classes.org - your site on safari