Search code examples
phpmysqlcolorshtml-tablealternate

PHP and mySQL table output row colors


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?


Solution

  • 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:

    $ping = 'pong';
    echo "Ping = $ping";
    /* pong */
    
    $ping = 'pong';
    echo 'Ping = $ping';
    /* $ping */
    
    $ping = 'pong';
    echo 'Ping = ' . $ping;
    /* pong */
    

    So in your case your code will be:

    <?php
        include('../htconfig/dbConfig.php'); // Get config document
        $mysqli = new mysqli($db['hostname'], $db['username'], $db['password'], $db['database']); // Connecting to SQL Server
    
        // Checking if connection was successfull
        if( $mysqli->connect_errno ){
            echo 'There was an error connection to the SQL Server<br>';
            echo '(' . $mysqli->connect_errno . ') ' . $mysqli->connect_error;
            exit; // FAIL
        }
    
        // Preparing a statement
        $stmt = $mysqli->prepare('SELECT * FROM tvdbase');
    
        // Checking if php prepared the statement successfully
        if(!$stmt){
            echo 'There was an error preparing the statement!';
            exit;
        }
    
        // Execute the statement
        if( !$stmt->execute() ){
            echo 'There was an error executing the statement!';
            exit;
        }
    
        // Catching data
        $result = $stmt->get_result();
    
        // Starting to create a string
        $str = '<table style="border:1px black solid;" >';
        $str .= '<tr>';
        $str .= '  <td style="border-bottom: 1px black solid;" >Date</td>';
        $str .= '  <td style="border-bottom: 1px black solid;" >Course</td>';
        $str .= '  <td style="border-bottom: 1px black solid;" >Room</td>';
        $str .= '</tr>';
    
        // Display data
        while($row = $result->fetch_array(MYSQLI_ASSOC)){
            $indx = $row['ID'];
            if ($indx % 2 == 0) {
                $bgColor = '#CCFFFF';
            } else {
                $bgColor = '#FFFF99';
            }
    
            $str .= '<tr>';
            $str .= '  <td style="background: ' . $bgColor . ';">' . $row['tvDate'] . '</td>';
            $str .= '  <td style="background: ' . $bgColor . ';">' . $row['tvCourse'] . '</td>';
            $str .= '  <td style="background: ' . $bgColor . ';">' . $row['tvRoom'] . '</td>';
            $str .= '</tr>';
    
        }
    
        // Print the string
        echo $str;
    ?>