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

?>

 

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.