Hi, I'm new to php so I hope this makes sense, thanks in advance for any help. I have setup a simple php database which prints out the data into a table. Is there away I can print out the data so each line in the table is alternative colours? example here of what I mean: My code so far, $data = mysql_query("SELECT * FROM repertoire") or die(mysql_error()); ?> <table width="685" border="0" cellpadding="5" cellspacing="0" class="reportoire"> <tr class="reportoireheader"> <td width="316" bgcolor="#F27072"><strong>Song</strong></td> <td width="347" bgcolor="#F27072"><strong>Original Artist</strong><strong></strong></td> </tr> <? while($row = mysql_fetch_array( $data )) print "<tr class='darkred'><td>".$row['song']."</td>"."<td>".$row['artist']."</td></tr>"; ?> </table>
Yeah here's what I do for Dollar Hauler <?php $result = mysql_query("SELECT * FROM repertoire") $x = 0; while($row = mysql_fetch_array($result)){ if ($x == 0){ $class = 'dark'; $x = 1; }elseif ($x == 1){ $x = 0; $class='light'; } echo '<tr><td [COLOR="Red"]class="'.$class.'"[/COLOR]>'.$row['song'].'</td><td [COLOR="Red"]class="'.$class.'"[/COLOR]>'.$row['artist'].'</td></tr>'; } ?> PHP: Naturally, you'll have to make a PHP class for dark and light, something like .light{ background-color: #FFFFFF; } .dark{ background-color: #000000; } Code (markup): Also, watch your HTML - it isn't proper to put attributes in single quotes as far as I know Oh, and the code you have for the table header won't work - TR can't have any attributes - if you want to use a class for a row in a table, you have to use it on each TD individually.
Your code: while($row = mysql_fetch_array( $data )) print "<tr class='darkred'><td>".$row['song']."</td>"."<td>".$row['artist']."</td></tr>"; Code (markup): New code: $x=0; while($row = mysql_fetch_array( $data )) { if ($x % 2 == 0) { $color = "darkred"; } else { $color = "lightred"; } print "<tr class='" . $color . "'><td>".$row['song']."</td>"."<td>".$row['artist']."</td></tr>"; $x++; } Code (markup): This script checks if x is even, and returns the color depending on the result, and then increases x by one so that you have a different result next time. Please note that I use a css class called "lightred" that you do probably not have defined in your css code. Create one with the color you want.
You can shorten that code down somewhat tguillea $result = mysql_query("SELECT * FROM repertoire") $x = 0; while($row = mysql_fetch_array($result)){ $class = ($x++ % 2 == 0) ? 'light' : 'dark'; echo '<tr><td class="'.$class.'">'.$row['song'].'</td><td class="'.$class.'">'.$row['artist'].'</td></tr>'; } PHP: