2014-10-30 42 views
1

我使用php/html顯示了一些來自mysql數據庫的值。在我的表中,我有一個字段「事務類型」(txn_type)。我在交易類型(購買和退款)中有兩種類型的交易。是否有可能根據交易類型中的值更改css類?例如基於表中的php/mysql查詢輸出值更改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> 

回答

2

您可以根據您的類型添加一個條件,如:

<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> 
+0

感謝您的支持....它按預期工作。 – 2014-10-30 10:39:05

1

您可以使用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> 
+0

感謝支持。我試過這個,但是我得到這個錯誤......你能幫忙嗎?錯誤:未定義的變量:在C:\ wamp \ www \ Projects \ CCDLB \ edidelindex.php行 – 2014-10-30 10:27:06

+0

上的標籤是否在調用之前包含了PHP代碼?在​​一行? – 2014-10-31 00:55:01

1

也許 -

<?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>'; 
?> 
+0

謝謝,我也試過這個。它的工作正常。 – 2014-10-30 10:40:00

0

如果您的病情處於名爲$ tx的變量中n_type 試試這個

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