以下 PHP 脚本从 MySQL 数据库中提取数据并将结果显示在 HTML 表格中。
<?php
require_once 'config.php';
// create connection
$conn = new mysqli($currentConfig['host'], $currentConfig['user'], $currentConfig['pass'],
$currentConfig['name']);
// check connection
if ($conn->connect_error) {
die('Connection failed: '.$conn->connect_error);
}
$sql = "SELECT Client, EstimateNumber, Status, TotalEstimatedTime, CostToDateRoleTotal, " .
"ROUND((CostToDateRoleTotal / TotalEstimatedTime) * 100) AS PercentComplete " .
"FROM Estimates " .
"WHERE Status != 'Invoiced' AND Status != 'Cancelled' AND TotalEstimatedTime > 0 " .
"AND CostToDateRoleTotal > 0 ORDER BY PercentComplete DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$client = $row['Client'];
$estimate_number = $row['EstimateNumber'];
$status = $row['Status'];
$total_estimated_time = $row['TotalEstimatedTime'];
$role_total = $row['CostToDateRoleTotal'];
$percent_complete = $row['PercentComplete'];
// echo data into HTML table
echo '<tbody>'.'<tr>'.'<td>'.$client.'</td>'.'<td>'.$estimate_number.'</td>'.'<td>'.
$status.'</td>'.'<td>'.$total_estimated_time.'</td>'.'<td>'.$role_total.'</td>'.
'<td>'.$percent_complete.' %'.'</td>'.'</tr>'.'</tbody>';
}
} else {
echo 'No results';
}
$conn->close();
我想添加一个<span>
类到 <td>
输出 $percent_complete
如果该单元格中的值大于 50,则为值。进行计算以创建该行数据的两种数据类型都是 DECIMAL。我尝试执行以下操作:
// echo flagged class to span element in table for percent complete
if ( $percent_complete > 50 ) echo $flag = '<span class="flagged">';
else echo '<span>';
然后我将 HTML 表格的 echo 语句编辑为:
// echo data into HTML table
echo
'<tbody>'.'<tr>'.'<td>'.$client.'</td>'.'<td>'.$estimate_number.'</td>'.'<td>'.$status.'</td>'.'<td>'.$total_estimated_time.'</td>'.'<td>'.$role_total.'</td>'.'<td>'.$flag.$percent_complete.' %'.'</span>'.'</td>'.'</tr>'.'</tbody>';
但是,这增加了一个 <span class="flagged">
到每一行。 var_dump()
的 $percent_complete
给我:
string(4) "1838" string(4) "1591" string(3) "592" string(3) "416" string(3) "367" string(3) "346" string(3) "305" string(3) "267" string(3) "266" string(3) "231" string(3) "193" string(3) "169" string(3) "157" string(3) "149" string(3) "142" string(3) "136" string(3) "134" string(3) "127" string(3) "114" string(3) "109" string(3) "106" string(3) "103" string(2) "96" string(2) "88" string(2) "71" string(2) "71" string(2) "70" string(2) "67" string(2) "59" string(2) "58" string(2) "46" string(2) "44" string(2) "38" string(2) "38" string(2) "37" string(2) "34" string(2) "18" string(2) "16" string(1) "9" string(1) "4""
这些是我表中所有行的字符串值。如何更改我以前的 if 语句以分别测试这些字符串值中的每一个?感谢您的帮助。
最佳答案
您的逻辑不成立,因为您定义了 $flag
如果 $percent_complete > 50
变量一次,但当它不是时,永远不要将它定义回空字符串。所以它的值是总是 <span class="flagged">
在之后的每次后续迭代中。
这段代码
if ( $percent_complete > 50 ) echo $flag = '<span class="flagged">';
else echo '<span>';
应该是
if ( $percent_complete > 50 ) {
$flag = '<span class="flagged">';
} else {
$flag = '<span>';
}
关于php - 使用 PHP 将类添加到 MySQL 数据的 HTML 表的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39301363/