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
https://forums.phpfreaks.com/topic/108877-quick-question/
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
https://forums.phpfreaks.com/topic/108877-quick-question/#findComment-558510
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
https://forums.phpfreaks.com/topic/108877-quick-question/#findComment-558521
Share on other sites

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.