Jump to content

Quick Question


paulman888888

Recommended Posts

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

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

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