簡體   English   中英

PHP和MySQL表輸出行顏色

[英]PHP and mySQL table output row colors

我已經建立了一個mySQL數據庫,並正在編寫PHP代碼以讀取表的內容並將其輸出為HTML表。 我想改變行的顏色,但是我很努力。 我已經在這里搜索了主題,並嘗試了所有發現的內容,但是沒有用。 這是我的代碼

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


?>

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM