2016-12-29 50 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这个东西