leachus2002 Posted May 27, 2010 Share Posted May 27, 2010 Hi There, I have written the code below, which happily returns all the rows, into a small table from an SQL select statement: <?php $linenumber = 1; $usercount = mssql_query("SELECT [username] FROM [mytable].[fullofusers] order by username asc;")or die(mssql_error()); ?> <table width="50%" cellspacing="0"> <tr> <td width="50"></td> <td height="20"><b>UserName</b></td> </tr> <?php while($userresults = mssql_fetch_array($usercount)){ echo "<tr>"; echo "<td>".$linenumber++."</td>"; echo "<td>".$userresults['username']."</td>"; echo "</tr>"; } ?> </table> As you can see, the table has just normal formatting. What I am wondering is if there is a way to say format every other row differently - for example, alternate colours? Many Thanks Matt Link to comment https://forums.phpfreaks.com/topic/203074-help-with-table-formatting/ Share on other sites More sharing options...
ixicoding Posted May 27, 2010 Share Posted May 27, 2010 Assuming you know some CSS/HTML you can use php to check a variable that adds to itself every time the loop is executed. If it is even then you use a certain color and a different one for odd. For example: <?PHP $i = 1; while($row = mysql_fetch_array($result)) { if ($i % 2 == 0) { echo "some info in the table here with a green background"; } else { echo "some info in the table here with a white background"; } $i = $i + 1; } ?> Link to comment https://forums.phpfreaks.com/topic/203074-help-with-table-formatting/#findComment-1064067 Share on other sites More sharing options...
leachus2002 Posted May 27, 2010 Author Share Posted May 27, 2010 That sounds great, thanks, How would I manage to echo the following in one block though: <?php echo "<tr>"; echo "<td>".$linenumber++."</td>"; echo "<td>".$userresults['username']."</td>"; echo "</tr>"; ?> for example, combine all those echo statements in to 1? Thanks Matt Link to comment https://forums.phpfreaks.com/topic/203074-help-with-table-formatting/#findComment-1064075 Share on other sites More sharing options...
ixicoding Posted May 27, 2010 Share Posted May 27, 2010 You mean: <?PHP echo "<tr><td>".$linenumber++."</td><td>".$userresults['username']."</td></tr>"; ?> ? Link to comment https://forums.phpfreaks.com/topic/203074-help-with-table-formatting/#findComment-1064079 Share on other sites More sharing options...
leachus2002 Posted May 27, 2010 Author Share Posted May 27, 2010 Oh yea!! What a fairy! Shows I am not thinking today! LOL Thanks for your help!! Link to comment https://forums.phpfreaks.com/topic/203074-help-with-table-formatting/#findComment-1064083 Share on other sites More sharing options...
ixicoding Posted May 27, 2010 Share Posted May 27, 2010 Lol, not a problem, good luck. Link to comment https://forums.phpfreaks.com/topic/203074-help-with-table-formatting/#findComment-1064085 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.