Here is my coding, I want to hide the result if value = 0, how to change to code
下面是代码
$lscSql = "SELECT item_name,qty FROM lsc JOIN item on item.item_id = lsc.item_id";
$lscResult = $connect->query($lscSql);
$lsc = $lscResult->fetch_all();
for ($x = 0; $x < count($lsc); $x++) {
$check = true;
$result = $connect->query($AllQtySql);
while ($row = $result->fetch_array()) {
if ($row['total'] != NULL) {
$item = "";
//(int) $row['total'] <= (int) $lsc[$x][1] &&
if ($row['item_name'] == $lsc[$x][0] && $check == true && $row['location_name'] == $location[1]) {
$num = count($lsc);
$table .= "<tr>
<th colspan='" . $num . "'>" . $row['item_name'] . "</th>
</tr><tr>";
$check = false;
}
if ($row['item_name'] == $lsc[$x][0] && $row['location_name'] == $location[1]) {
$table .= "
<td>" . $row['device_name'] . " (";
if ((int) $row['total'] <= (int) $lsc[$x][1]) {
$table .= "<span style='color:red'>" . $row['total'] . "</span>)</td>";
} else {
$table .= $row['total'] . ")</td>";
}
}
}
}
}
$table .= "</tr></tbody></table>";
echo $table;
我需要做的就是在value = 0时隐藏结果
答案 0 :(得分:0)
基本上,您需要检查项目数量,如果数量为0,则输出空的<td>
而不是device_name
和total
。如果我正确理解了您的代码,请替换此代码:
if ($row['item_name'] == $lsc[$x][0] && $row['location_name'] == $location[1]) {
$table .= "<td>" . $row['device_name'] . " (";
if ((int) $row['total'] <= (int) $lsc[$x][1]) {
$table .= "<span style='color:red'>" . $row['total'] . "</span>)</td>";
} else {
$table .= $row['total'] . ")</td>";
}
}
使用此代码应该可以工作:
if ($row['item_name'] == $lsc[$x][0] && $row['location_name'] == $location[1]) {
if ($row['total'] == 0) {
$table .= "<td></td>";
}
else {
$table .= "<td>" . $row['device_name'] . " (";
if ((int) $row['total'] <= (int) $lsc[$x][1]) {
$table .= "<span style='color:red'>" . $row['total'] . "</span>)</td>";
} else {
$table .= $row['total'] . ")</td>";
}
}
}
答案 1 :(得分:0)
我认为最简单的解决方案是修改查询以排除总数为0
的项目。假设列total
在item
表中,则您的$lscSql
查询应修改为:
SELECT item_name,qty FROM lsc JOIN item on item.item_id = lsc.item_id;
到
SELECT item_name, qty FROM lsc JOIN item ON (item.item_id = lsc.item_id) WHERE item.total = 0;
为防止删除lsc
中的记录出现问题,您应该改用LEFT JOIN
:
SELECT item_name, qty FROM lsc
LEFT JOIN item ON (item.item_id = lsc.item_id)
WHERE item.total = 0;