Zend - The PHP Company




Tables

Add Code


Alternate Row colors in a table  

Type: code fragment
Added by: pauldy
Entered: 13/05/2000
Last modified: 08/12/1999
Rating: **** (11 votes)
Views: 17781
This is a simple way to alternate row colors in php


<?php
function row_color($i) {
  
$bgcolor1 "#222222";
  
$bgcolor2 "#444444";
  if ( (
$i 2) == ) {
    return 
$bgcolor1;
  } else {
    return 
$bgcolor2;
  }
}
?>


Usage Example


<table>
  <tr bgcolor="#000099"><td>Company Name</td><td>Company Address</td><td>Company Phone</td></tr>
<?php

$query 
"select company_name,company_address,company_phone from companyinfo";
$result MYSQL_QUERY($query);
$numrows MYSQL_NUMROWS($result);
$numfields mysql_num_fields($result);
$i "0";

WHILE (
$i $numrows ) {
  
$j "0";

  WHILE (
$j $numfields) {

    
$field mysql_field_name($result,$j);
    $
$field mysql_result($result,"$i",$field);
    
$j++;

  }

  
$i++;

?>

<tr bgcolor="<?php row_color($i); ?>"><td><?php echo $company_name?></td><td><?php echo $company_address?></td><td><?php echo $company_phone?></td></tr>
<?php

}

?>

</table>


Rate This Script





Search



This Category All Categories