我正在使用以下查询,我想根据我查询的一个项目的值更改div颜色。在下面的代码中,我试图检查mgap_accept的值。如果值为1,则结果将以不同的div输出,如果值不是1。
我是用IF?ELSE做的,但我不确定是否有更好的方法。这是我的代码:
$result_cat = "SELECT mgap_accept,mgap_ska_id FROM mgap_orders WHERE mgap_ska_id = '$id'";
$stmt = $pdo->prepare($result_cat);
$stmt->execute();
while($row_cat = $stmt->fetch(PDO::FETCH_ASSOC))
{
$id1=$row_cat['mgap_ska_id'];
$accept=$row_cat['mgap_accept'];
$growth=($total + $recovery);
if($accept == '1'){
?>
<div class="show">
<span class="namecustcoltype"><?php echo $id1; ?></span>
<span class="namecusttype"><?php echo $accept; ?></span>
<span class="growthcust">$<?php echo number_format($growth); ?></span>
</div>
<?php
}else{
?>
<div class="showop">
<span class="namecustcoltype"><?php echo $id1; ?></span>
<span class="namecusttype"><?php echo $accept; ?></span>
<span class="growthcust">$<?php echo number_format($growth); ?></span>
</div>
<?php
}
?>
答案 0 :(得分:1)
<div class="<?php echo ($accept == '1')?'show':'showop';?>">
“?:” - ternary operator。
答案 1 :(得分:1)
$result_cat = "SELECT mgap_accept,mgap_ska_id FROM mgap_orders WHERE mgap_ska_id = '$id'";
$stmt = $pdo->prepare($result_cat);
$stmt->execute();
while($row_cat = $stmt->fetch(PDO::FETCH_ASSOC))
{
$id1=$row_cat['mgap_ska_id'];
$accept=$row_cat['mgap_accept'];
$growth=($total + $recovery);
$class = $accept == '1' ? "show" : "showop";
?>
<div class="<?=$class?>">
<span class="namecustcoltype"><?php echo $id1; ?></span>
<span class="namecusttype"><?php echo $accept; ?></span>
<span class="growthcust">$<?php echo number_format($growth); ?></span>
</div>
答案 2 :(得分:0)
您将始终需要if else
声明。
但是你可以缩短你的代码:
$accept == '1'? $class="show" : $class="showop;
?>
<div class="<?php echo $class; ?>">
<span class="namecustcoltype"><?php echo $id1; ?></span>
<span class="namecusttype"><?php echo $accept; ?></span>
<span class="growthcust">$<?php echo number_format($growth); ?></span>
</div>