2016-12-29 73 views
0

我已經建立了一個mySQL數據庫,並且正在編寫PHP代碼來讀取表格的內容並將其輸出爲HTML表格。我想交替行的顏色,但我很努力這樣做。我在這裏搜索了這些主題,並嘗試了我發現的所有內容,但這不起作用。這是我的代碼PHP和mySQL表格輸出行顏色

<?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列(日期課程房間),但不是顏色。

請幫忙嗎?

+0

如果我是正確的,你正試圖使這些行獲得與'$ bgColor'可變背景顏色對? – 2016-12-29 17:42:23

+0

如果你想變換顏色,使用CSS的':nth-​​child'選擇器會更容易。否則,你只是缺少td中變量的$。 – aynber

回答

0

你需要做的是要記住一個變量之前使用$

另外,可以在雙引號字符串中使用$,但不能用單引號引起來。

例子:

$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; 
?> 
+0

輝煌,謝謝。效果很好。 – Alex

+0

沒問題Alex – 2016-12-30 06:04:19

0

你忘了把$符號的bgcolor變量

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

之前,但它能夠更好地使用CSS這個東西