我已经设置了一个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列(Date-Course-Room)但不是颜色。
请帮忙吗?
答案 0 :(得分:0)
你忘了在bgColor变量之前加上$ sign
echo "
<tr>
<td ".$bgColor .">" . $row['tvDate'] . "</td>
<td ".$bgColor .">" . $row['tvCourse'] . "</td>
<td ".$bgColor .">" . $row['tvRoom'] . "</td>
</tr>";
但是使用css这个东西更好
答案 1 :(得分: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;
?>