如果结果值= 0,如何隐藏

时间:2019-10-02 01:29:50

标签: php mysql database user-interface

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时隐藏结果

enter image description here

2 个答案:

答案 0 :(得分:0)

基本上,您需要检查项目数量,如果数量为0,则输出空的<td>而不是device_nametotal。如果我正确理解了您的代码,请替换此代码:

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的项目。假设列totalitem表中,则您的$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;