首页 > 解决方案 > 我如何在 php 中从 mysql 查询结果组按日期给出符号(如更改颜色或 fa-up/fa-down)

问题描述

在此处输入图像描述

预期:如果 osl 高于之前的日期,则回显“fa-up”。

<?php $sql_nongadai_kol_kw2= mysqli_query($kon,"SELECT * FROM vw_nongadai_kanwil ORDER BY TGL_UPDATE DESC LIMIT 31"); ?> 
<?php foreach( $sql_nongadai_kol_kw2 as $print ){;
    $tanda ='tes';
    $osl_tes =$print['osl_total'];
    if ($osl_tes > $osl_tes) {$tanda = '<i class="fa fa-long-arrow-up"></i>';}
    elseif ($osl_tes < $osl_tes) {$tanda = '<i class="fa fa-long-arrow-down"></i>';}
    elseif ($osl_tes = $osl_tes) {$tanda = '<i class="fa fa-long-arrow-v"></i>';}
    else {$tanda = 'error';}
?>
    <tr>
      <th scope="row"><?php echo $print['tgl_update'];?></th>
      <td align="right"><?php echo $tanda;?> - <?php echo number_format($print['osl_total']); ?></td>                            
    </tr>                                
<?php } ;?>

标签: phphtmlcssmysql

解决方案


您应该$osl_tes在 foreach 循环之前启动,在循环内比较$osl_tes并将$print['osl_total']新值绑定到$osl_tes

<?php 
    $sql_nongadai_kol_kw2= mysqli_query($kon,"SELECT * FROM vw_nongadai_kanwil ORDER BY TGL_UPDATE DESC LIMIT 31"); 
    $osl_tes = 0; // or some other initial value
?> 
<?php foreach( $sql_nongadai_kol_kw2 as $print ){;
    $tanda ='tes';
    
    if ($print['osl_total'] > $osl_tes) {$tanda = '<i class="fa fa-long-arrow-up"></i>';}
    elseif ($print['osl_total'] < $osl_tes) {$tanda = '<i class="fa fa-long-arrow-down"></i>';}
    elseif ($print['osl_total'] = $osl_tes) {$tanda = '<i class="fa fa-long-arrow-v"></i>';}
    else {$tanda = 'error';}

    $osl_tes = $print['osl_total']; // bind new value
?>
    <tr>
      <th scope="row"><?php echo $print['tgl_update'];?></th>
      <td align="right"><?php echo $tanda;?> - <?php echo number_format($print['osl_total']); ?></td>                            
    </tr>                                
<?php } ;?>

推荐阅读