Re: How to format every secound row in a database result

From: Date: Wed, 30 Mar 2005 16:17:48 +0000
Subject: Re: How to format every secound row in a database result
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-211890@lists.php.net to get a copy of this message
Georg wrote:
Hi! I wish to format the output of a database select. The result is presented in a table, in which i'd like to present everey secound row with a different background color. My idea was to test if the remaining number of rows where a prime number or
modulo 2 .... is the current row 'number' divisible by 2 with no remainder? (i.e. is it an odd row or an even row) hint code: $i = 1; // assume $rows is your DB resultset foreach($rows as $row) { $bgcolor = ($i % 2) ? '#fff': '#f00'; $i++; $yourColumns = ''; foreach ($row as $field) { $yourColumns =. "<td>$field</td>"; } echo '<tr style="'.$bgcolor.'">',$yourColumns,'<tr>'; } I really think that prime numbers are not what your looking for.... but maybe you have very specific formatting requirements?!!??
not, and by that determent if the background color should be white or som ivory-like color to improve the readability in the result table... Hope to hear from anyone with experience in similar matters...
btw there are quite a few javascript libs that do auto coloring of HTML tables in the way you describe - they are pretty much drag'n'drop scripts - I'll leave finding them up to you ;-)
TIA! Georg Herland, Norway


« previous php.general (#211890) next »