Zend - The PHP Company




Tables

Add Code


Alternating colours with unlimited colours...  

Type: code fragment
Added by: richardsmith
Entered: 14/11/2000
Last modified: 01/12/2000
Rating: *** (5 votes)
Views: 6888

This nifty little pice of code is probably the best method, because it means you can have unlimted colours, 4, 4, or 5 colours...

There's nothing complicated about it either... it's but 2 lines of code at the end of a loop...



<table cellpadding=0 cellspacing=0 border=0 width=100%>

<?
    
// Define the colours...
    
$colours   = array();
    
$colours[] = "#EDEDED";
    
$colours[] = "#CCCCCC";
    
$colours[] = "#FFFFFF";
    
// Loop through from 10 -> 15
    
for ($i 1$i <= 15$i++){
    
        
        
// Print the row with the colour value...
        
printf("<tr bgcolor="%s"><td>$i</td></tr>n"$colours[0]);
        
        
        
// Now for the magic... First we take the last value off the end of the array
        // and pop it onto the beginning...
        
array_unshift($colours$colours[count($colours)-1]);
        
// array_unshift does this nicley, and I've added the "count($colours)-1"
        // bit so you can have as many colours as you need...
        
        // Then we remove the last element of the array... 
        
array_pop($colours);
    
    }


?>

</table>


Usage Example




Rate This Script





Search



This Category All Categories