【问题标题】:change the css class based on the php/mysql query output values in a table根据表中的 php/mysql 查询输出值更改 css 类
【发布时间】:2014-10-30 09:53:41
【问题描述】:

我正在使用 php/html 显示 mysql 数据库中的一些值。在我的表中,我有一个字段“交易类型”(txn_type)。我在交易类型字段中有两种交易类型(购买和退款)。是否可以根据事务类型中的值更改 css 类?例如

if the value is purchase I need to execute this: 

    <td><span class="label label-info label-mini"><?php echo $type ?></span></td>

If it is Refund I need to execute this:

    <td><span class="label label-success label-mini"><?php echo $type ?></span></td>.

如果可能,请尽可能。 附上我的代码。

<table class="table table-striped table-advance table-hover" id="dataTables-example">
<thead>
<tr>
<th><i class="fa fa-bullhorn"></i> Type</th>
<th class="hidden-phone"><i class="fa fa-calendar"></i> Date</th>
<th><i class="fa fa-rupee"></i> Amount</th>
<th><i class=" fa fa-book"></i> Details</th>
<th><i class=" fa fa-calendar-o"></i> Due Date</th>
<th><i class=" fa fa-history"></i> Action</th>

</tr>
</thead>
<tbody>
<?php
$query = mysqli_query($GLOBALS["___mysqli_ston"], "select * from dlbcc_purchase order by date(purch_date)desc")or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
while ($row = mysqli_fetch_array($query)) {
$id = $row['id'];
$type = $row['txn_type'];
$date = $row['purch_date'];
$amount = $row['purch_amt'];
$dtls = $row['purch_dtls'];
$due = $row['due_date'];
?>

<tr>
<td><span class="label label-success label-mini"><?php echo $type ?></span></td>
<td><?php echo $date ?></td>
<td><?php echo $amount?></td>
<td><?php echo $dtls ?></td>
<td><span class="label label-warning label-mini"><?php echo $due ?></span></td>
<td>

<button class="btn btn-primary btn-xs"><i class="fa fa-pencil"></i></button>
<button class="btn btn-danger btn-xs"><i class="fa fa-trash-o "></i></button>
</td>
</tr>
<?php } ?>
</tbody>
</table>

【问题讨论】:

    标签: php mysql css


    【解决方案1】:

    您可以根据您的类型添加条件,如下所示:

    <tr>
    <td><span class="label <?php if ($type == 'purchase') { echo 'label-info'; } elseif ($type == 'refund') { echo 'label-success'; }?> label-mini"><?php echo $type ?></span></td>
    <td><?php echo $date ?></td>
    <td><?php echo $amount?></td>
    <td><?php echo $dtls ?></td>
    <td><span class="label label-warning label-mini"><?php echo $due ?></span></td>
    <td>
    

    【讨论】:

      【解决方案2】:

      您可以使用 PHP 来执行此操作。比如:

      <?php
      if ($type == "purchase") echo $label="label-info";
      else if ($type == "refund") echo $label = "label-success";
      else // provide some fallback here, if there are more types of transaction
      ?>
      

      然后回显/显示正确的类:

      <td><span class="label <?php echo $label ?> label-mini"><?php echo $type ?></span></td>
      

      【讨论】:

      • 感谢您的支持。我已经尝试过了,但我收到了这个错误......你能帮忙吗?错误:未定义的变量:C:\wamp\www\Projects\CCDLB\edidelindex.php 中的标签在线
      • 您在调用 PHP 代码之前是否包含了它?在 行?
      【解决方案3】:

      也许-

      <?php 
      
         if ( $type == 'purchase ') {
            echo '<td><span class="label label-info label-mini"> ';
         } else {
            echo '<td><span class="label label-success label-mini"> ';
         }
         echo $type
         echo '</span></td>';
      ?>
      

      【讨论】:

      • 谢谢,我也试过这个。它按预期工作正常。
      【解决方案4】:

      如果您的条件位于名为 $txn_type 的变量中 试试这个

      <td class="<?php echo ($txn_type == 'purchase')?("label label-info label-mini":($txn_type == 'Refund')?"label label-success label-mini":"")?>" >
      

      【讨论】:

        猜你喜欢
        • 2016-11-16
        • 1970-01-01
        • 2014-09-16
        • 2019-12-12
        • 1970-01-01
        • 2015-04-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多