PHP 和 mySQL table 输出行颜色

PHP and mySQL table output row colors

我已经设置了一个 mySQL 数据库并正在编写 PHP 代码来读取 table 的内容并将其输出为 HTML table .我想交替使用行颜色,但我正在努力这样做。我在这里搜索了主题并尝试了我发现的所有内容,但它不起作用。这是我的代码

<?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();
}


?>

它显示我的 table 有 3 列(日期-课程-房间),但没有显示颜色。

有什么帮助吗?

你忘了在 bgColor 变量前加上 $ 符号

echo "
<tr>
<td ".$bgColor .">" . $row['tvDate'] . "</td>
<td ".$bgColor .">" . $row['tvCourse'] . "</td>
<td ".$bgColor .">" . $row['tvRoom'] . "</td>
</tr>"; 

但最好使用 css 来处理这些东西

你需要做的是记得在变量前使用$

此外,可以在双引号字符串中使用 $,但不能在单引号中使用。

示例:

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

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

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

因此在您的情况下,您的代码将是:

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