Re: % Help

From: Date: Wed, 21 Feb 2001 05:59:19 +0000
Subject: Re: % Help
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-40892@lists.php.net to get a copy of this message
On Wed, 21 Feb 2001 16:01, Jeff Lacy wrote: > Hello Everyone, > > Could someone please explain the whole % thing? I sort of understand > it, but not quite. My goal is to have a table, and have every row > alternate between 4 colors. I can alternate sort of alternate between > 3, but not quite. I have tried lots of different combinations, but I > can't figure it out. Thank you very much! > > > Maybe there should be a better link to it in the manual. The only one > I could find was at http://www.php.net/manual/en/ref.math.php, > but it > is far from clear (to me at least....). > > Jeff Lacy > > P.S. Please email me directly, and respond to the list because I am > not really subscribed to it. When I look at the newsgroup, it crashes > Outlook Express, and the list is VERY high volume (for me). Thanks > again. Perhaps you should be looking in Arithmetical Operators www.php.net/manual/en/language.operators.php The modulus operator gives you the value of the remainder after dividing a by b, so 4 % 3 will give a result of 1; ie 3 goes into 4 once and leaves a remainder of 1 Or 10 % 3 also gives a result of 1; 20 % 6 gives 2 etc. Now for your particular problem of alternating table colors, you don't necessarily need the modulus operator, but using it you could implement thus: $color = array('#ffffff'=>0, 'blue'=>1, 'red' =>2, 'green' =>3); $row_num = 0; $how_many_colors = 4; while { //Build your row producing logic here echo "<TR BGCOLOR=\"$color[$row_num % $how_many_colors]\">"; // Rest of row info $row_num++; } Mind you, there might be the odd syntax error there; I didn't test this at all :-) but hopefully you get the idea. The value of $row_num % $how_many_colors will cycle through 0 - 3 and act as the subscript for the array of colours. If you want more colours, just increase the number of values in the array and adjust $how_many_colors accordingly. 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.general (#40892) next »