I have setup a mySQL database and am writing PHP code to read the contents of a table and output it as an HTML table. I want to alternate the row colors but I am struggling to do so. I have searched the topics here and tried everything I found but it's not working. Here is my code
<?php
{ // Secure Connection Script
include('../htconfig/dbConfig.php');
$dbSuccess = false;
$dbConnected = mysql_connect($db['hostname'],$db['username'],$db['password']);
if ($dbConnected) {
$dbSelected = mysql_select_db($db['database'],$dbConnected);
if ($dbSelected) {
$dbSuccess = true;
}
}
// END Secure Connection Script
}
if ($dbSuccess) {
$query = "SELECT * FROM tvdbase";
$result = mysql_query($query);
echo "<table>";
echo "<table border='1'>";
echo "<tr>";
echo "<td>Date</td>";
echo "<td>Course</td>";
echo "<td>Room</td>";
echo "</tr>";
$indx = 0;
while($row = mysql_fetch_array($result)){
$indx = $row['ID'];
if ($indx % 2 == 0) {
$bgColor = ' style="background-color:#CCFFFF;" ';
} else {
$bgColor = ' style="background-color:#FFFF99;" ';
}
echo "
<tr>
<td bgColor>" . $row['tvDate'] . "</td>
<td bgColor>" . $row['tvCourse'] . "</td>
<td bgColor>" . $row['tvRoom'] . "</td>
</tr>";
$indx++;
}
echo "</table>";
mysql_close();
}
?>
It shows my table with the 3 columns (Date-Course-Room) but not the colors.
Any help please?
What you need to do is to remember to use
$
before a variable.Also, it is possible to use
$
inside a double quoted string, but not in single quoted.Examples:
So in your case your code will be: