1

I am displaying some values from mysql database by using php/html. In my table, i have a field "type of transaction"(txn_type). I have two type of transactions in the type of transaction field (purchase and refund). Is it possible to change the css class bases on value in transaction type? for example

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>.

Please possible if possible. Attaching my code.

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

4 Answers 4

2

You can add a condition based on your type, like this:

<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>
Sign up to request clarification or add additional context in comments.

Comments

1

You can use PHP to do this. Something like:

<?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
?>

Then echo/display the correct class:

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

2 Comments

thanks for the support. I have tried this, but i am getting this error... could you pls help. Error: Undefined variable: label in C:\wamp\www\Projects\CCDLB\edidelindex.php on line
did you include the PHP codes before it is being called? in the <td> line?
1

Maybe -

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

1 Comment

thanks, i have tried this also. its working fine as expected.
0

if your condition is in a varible called $txn_type try this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.