LimitedStreak Posted August 19, 2011 Share Posted August 19, 2011 how to make a different colour for each row like odd, even, odd, even etc does anyone know how todo it and the table includes data from database thanks Quote Link to comment Share on other sites More sharing options...
Anonymous Posted August 19, 2011 Share Posted August 19, 2011 (edited) echo "<table>"; $sql = "SELECT field1, field2, field3 FROM table ORDER BY field1 LIMIT 20"; $rs = mysql_query($sql); $odd = 1; while ($row = mysql_fetch_array($rs)) { echo sprintf('<tr class="%s">', $odd ? "odd" : "even"); # echo "<tr>"; commented out to appease the pedantic fool below echo "<td>" . htmlentities($row[0]) . "</td>"; echo "<td>" . htmlentities($row[1]) . "</td>"; echo "<td>" . htmlentities($row[2]) . "</td>"; echo "</tr>"; $odd = 1 - $odd; } mysql_free_result($rs); echo "</table>"; Edited August 19, 2011 by Anonymous Quote Link to comment Share on other sites More sharing options...
LimitedStreak Posted August 19, 2011 Author Share Posted August 19, 2011 (edited) mate i really not sure what you did. Its a table just say this, where would i put the odd, even? <table width="300" border="0" cellpadding="0" cellspacing="0" class="table"> <tr class="thinline" class="table_back"> <td colspan=2> Last 10 Registered</td> </tr> <tr class=table> <td class="subhead" width="109">Username</td> <td class="subhead" width="126">Date/time</td> </tr> <? $c=mysql_query("SELECT * FROM users ORDER BY id DESC LIMIT 10"); while($d=mysql_fetch_object($c)){ echo "<tr><td align=left><a href='profile.php?viewuser=$d->username'>$d->username</a></td><td>$d->regged</td></tr>"; } ?> </table></td> Edited August 19, 2011 by Nickson added code tags Quote Link to comment Share on other sites More sharing options...
Nickson Posted August 19, 2011 Share Posted August 19, 2011 Added code tags, please use these in the future, as well as remove unneeded empty lines... Also, this is basic html knowledge in combination of nested tables. Anonymous has posted an example of how you could do it properly, of course you need to edit that code to suit YOUR needs. But I'm quite sure you'll have to do that yourself... Afterall, if you can own a game, you should be able to do this by yourself, it's not rocket science ;) Quote Link to comment Share on other sites More sharing options...
Anonymous Posted August 19, 2011 Share Posted August 19, 2011 There is one key feature to it: echo sprintf('<tr class="%s">', $odd ? "odd" : "even"); which suggests you do similar. You are echo'ing a <tr> element ... change yours to suit. echo "<tr class=\"xxx\"><td align=left><a href='profile.php?viewuser=$d->username'>$d->username</a></td><td>$d->regged</td></tr>"; Replace xxx with odd/even to suit. You will of course have to provide a mechanism to flip odd to even and back again - and of course the css to support odd/even rows, but that I leave as an exercise. While I don't mean to sound harsh - perhaps the "Website Developer - Willing to help you!" tag in your sig is overly optimistic? Quote Link to comment Share on other sites More sharing options...
bluegman991 Posted August 19, 2011 Share Posted August 19, 2011 (edited) It would probably be easier to do it in css [CSS]table > tbody > td:odd { background-color:#0f0; } table > tbody > td:even { background-color:#f00; }[/CSS] Edited August 19, 2011 by bluegman991 Quote Link to comment Share on other sites More sharing options...
Anonymous Posted August 19, 2011 Share Posted August 19, 2011 Indeed, but some pedants here dislike people suggesting CSS-3. At least this way there's a chance it will work in everything back to pre IE6 days. Quote Link to comment Share on other sites More sharing options...
lucky3809 Posted August 19, 2011 Share Posted August 19, 2011 I do exactly the opposite ... add this under your while statement...as shown in example... while($d=mysql_fetch_object($c)){ $bgcolor = ($bgcolor == "#ececec") ? "#ffffff" : "#ececec"; then for your <tr> tag add... <tr style='background-color: ".$bgcolor ."'> and your done, really simple and easy! Quote Link to comment Share on other sites More sharing options...
LimitedStreak Posted August 20, 2011 Author Share Posted August 20, 2011 Thank You All, i have found the solution by lucky 3809 ;) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.