Re: [PHP4BETA] A (Hopefully) simple question *gulp*

From: Date: Fri, 28 Apr 2000 03:20:59 +0000
Subject: Re: [PHP4BETA] A (Hopefully) simple question *gulp*
References: 1  Groups: php.version4 
Request: Send a blank email to php-version4+get-14130@lists.php.net to get a copy of this message
On 27 Apr, John Steele wrote: > David, > >>Yup. Initialise a counter then increment it for each table row you >>display. Then, on each row, test the counter value mod 2. If the result >>is zero, use one colour for row background, otherwise use the other >>colour. > > Here's another simple way to let the colors do the work. It can be made into a pretty > sick one-liner :) > > <? > $color1 = "#ffffff"; > $color2 = "#E1DAC1"; > ?> > <TABLE BORDER=0 CELLSPACING=1 CELLPADDING=1> > <TR> > <TH>Blah</TH><TH>Blah2</TH> > </TR> > <? > $color = ""; > for ($i=0;$i < $num; $i++) { > $color = ($color == $color1) ? $color2 : $color1; > echo "<tr > bgcolor=\"$color\"><td>yada</td><td>spew</td></tr> > // more stuff > } > ?> > </table> Neat - it cuts out the 'increment the counter' phase and saves a line or two of code :-). Just out of sheer curiosity, I wonder which would be more CPU-effective; doing the counter increment and a modulus/numeric comparison in the ternary; or the string comparison in the ternary. I know it's academic in respect of the computing power we have these days, but being brought up on machines with limited memory and slow processors.... Old habits die hard. Cheers -- David Robley | WEBMASTER & Mail List Admin RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/ AusEinet | http://auseinet.flinders.edu.au/ Flinders University, ADELAIDE, SOUTH AUSTRALIA

« previous php.version4 (#14130) next »