paulman888888 Posted June 5, 2008 Share Posted June 5, 2008 I have my php and mysql set up but i want it to be a pretty table. So i would like it to be more like this <table width="100%" border="1"> <tr bgcolor="#33FF00"> <td> </td> <td> </td> </tr> <tr bgcolor="#0000FF"> <td> </td> <td> </td> </tr> </table> Where the rows change colour from green to blue. How would i get my php code to do that? <?php // Get all the data from the "example" table $result = mysql_query("SELECT * FROM example ORDER BY score DESC") or die('O no. Theres an error'); echo "<table border='1'>"; echo "<tr><th>ID</th><th>Name</th><th>Score</th><th>Ip address</th><th>Date</th></tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr>\n"; echo "<td><a href='delete.php?id=".$row['id']."'>".$row['id']."</a></td>\n"; echo "<td>".$row['name']."</td>\n"; echo "<td>".$row['score']."</td>\n"; echo "<td>".$row['ipaddress']."</td>\n"; echo "<td>".$row['date']."</td>\n"; echo "</tr>\n"; } echo "</table>"; ?> At the moment all the table backgrounds are the same. thankyou Link to comment https://forums.phpfreaks.com/topic/108877-quick-question/ Share on other sites More sharing options...
wildteen88 Posted June 5, 2008 Share Posted June 5, 2008 Change: while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr>\n"; echo "<td><a href='delete.php?id=".$row['id']."'>".$row['id']."</a></td>\n"; echo "<td>".$row['name']."</td>\n"; echo "<td>".$row['score']."</td>\n"; echo "<td>".$row['ipaddress']."</td>\n"; echo "<td>".$row['date']."</td>\n"; echo "</tr>\n"; } echo "</table>"; to $i = 0; // setup a counter while($row = mysql_fetch_array( $result )) { // alternate colors $color = ($i%2 == 0) ? '#33FF00' : '#0000FF'; // Print out the contents of each row into a table echo "<tr style=\"background-color: $color;\">\n"; echo "<td><a href='delete.php?id=".$row['id']."'>".$row['id']."</a></td>\n"; echo "<td>".$row['name']."</td>\n"; echo "<td>".$row['score']."</td>\n"; echo "<td>".$row['ipaddress']."</td>\n"; echo "<td>".$row['date']."</td>\n"; echo "</tr>\n"; $i++; // increment counter } echo "</table>"; Link to comment https://forums.phpfreaks.com/topic/108877-quick-question/#findComment-558510 Share on other sites More sharing options...
paulman888888 Posted June 5, 2008 Author Share Posted June 5, 2008 and to do class i would do this $i = 0; // setup a counter while($row = mysql_fetch_array( $result )) { // alternate colors $class = ($i%2 == 0) ? 'tabletype1' : 'tabletype2'; // Print out the contents of each row into a table echo "<tr style=\"class: $class;\">\n"; echo "<td><a href='delete.php?id=".$row['id']."'>".$row['id']."</a></td>\n"; echo "<td>".$row['name']."</td>\n"; echo "<td>".$row['score']."</td>\n"; echo "<td>".$row['ipaddress']."</td>\n"; echo "<td>".$row['date']."</td>\n"; echo "</tr>\n"; $i++; // increment counter } echo "</table>"; is that right? Link to comment https://forums.phpfreaks.com/topic/108877-quick-question/#findComment-558521 Share on other sites More sharing options...
wildteen88 Posted June 5, 2008 Share Posted June 5, 2008 Yes you are partly right. This echo "<tr style=\"class: $class;\">\n"; should be: echo "<tr class=\"$class\">\n"; Link to comment https://forums.phpfreaks.com/topic/108877-quick-question/#findComment-558525 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.