Jump to content

Help with Table Formatting


leachus2002

Recommended Posts

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

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;
}

?>

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.