HTML/PHP/CSS How to have alternate colours for table rows/columns

Seeruk

Senior member
Nov 16, 2003
986
0
0
Like it says on the box ... how can I programatically get alternate background colours for either the rows or columns to make the table easier to read?

I'm using PHP to query a DB and produce results that could be as wide as 40 fields, and with upto 200 results.

Of course there could be a lot fewer results depending on the query, so I figure there must be a way using either php or the css that can say all odd numbered columns = grey, all even numbered columns = silver .... or something similar?
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
Originally posted by: Seeruk
Like it says on the box ... how can I programatically get alternate background colours for either the rows or columns to make the table easier to read?

I'm using PHP to query a DB and produce results that could be as wide as 40 fields, and with upto 200 results.

Of course there could be a lot fewer results depending on the query, so I figure there must be a way using either php or the css that can say all odd numbered columns = grey, all even numbered columns = silver .... or something similar?

$result = mysql_query("your query here");

echo "<table>";

$i = true;

while($row = mysql_fetch_assoc($result)) {

echo "<tr>";

if($i) {

echo "<td bgcolor='#XXXXXX'>";

else {

echo "<td bgcolor='#YYYYYY'>";

}


//echo your data here

echo "</td>";

$i = !$i;

echo "</tr>";

}

echo "</table>";
 

Seeruk

Senior member
Nov 16, 2003
986
0
0
Actually (this is my 1st dabble in PHP world!) I think I am going about producing the table in a different way using appends to a $view_db variable before echoing it at the end. Not sure how to piece your method together with mine

There is probably an easier way...?
 

imported_jb

Member
Sep 10, 2004
171
0
0
if you are going to use the append code, and if it's a set number of rows, you might as well add the color html right in there.
using the example Zug gave would be necessary if you didn't know what the # of rows were every time.
so i',m suggesting:

Code:
$view_db .= '<td bgcolor='#XXXXXX'>'.$row['ShipName'].'</td>';
$view_db .= '<td bgcolor='#YYYYYYY'>'.$row['ShipWreck'].'</td>';

just because it is only used for exactly 39 rows. if you used a method similar to Zag'z, you'd be manually setting True or False between every append.
 

Seeruk

Senior member
Nov 16, 2003
986
0
0
Yeah I know... that's just it really because there will be an unkown number of rows. This is just a select * to test the db and format things in a worse case scenario (i.e. 89 rows and 40 columns are as big as it's gonna get for now).

Once all that is done I will be fancying things up a bit by adding filters and ability to custom query where the number of rows and columns will become unkown.

I suppose an additional question which I will need to address in the future is also a way to display the colum headings programatically too.

is

SELECT ShipName AS "Ship Name"

valid?


And I think I have seen a method of outputting to table but cannot find it... learning via the web can be a real trawl sometimes :)
 

dukdukgoos

Golden Member
Dec 1, 1999
1,319
0
76
Also, it's best practice to use CSS for styling instead of deprecated attibutes like bgcolor...
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
Not really an elegant solution, but it works (unless i made a typo?):
(going to edit the message to attach code -- still haven't figured out a way to attach code in quick reply >( ...)

Btw, are you thinking of alternating the color with each row, each column, or each cell?

Edit:
What dukdukgoos said about using CSS is true.. It's also more recommended, but the logic behind alternating CSS class works the same as attached
 

Seeruk

Senior member
Nov 16, 2003
986
0
0
Hmmm interesting stuff - Will give it another stab today if only damn users would stop calling me asking me to actually do real work!!!

And yeah it will all be thrown in a css at the end.

Thanks guys :D