简体   繁体   中英

How to give a color to a perticular column data in codeigniter?

 <? foreach($customer as $customer_details) {?> <tr id="customer_details_<?=$customer_details['id']?>"> <? foreach($dyncust_fields as $dyncust_field) { if($dyncust_field['add_to_listing']=='1') { echo "<td style='color:green;'>".$customer_details[$dyncust_field['attribute_name']]."</td>"; } }?> </tr> <? }?>

Here I written some code to display data of dynamic column, here I want to give a color for perticular data of perticular column. but it's not working. Here this line $customer_details[$dyncust_field['attribute_name']] is used to get the table record based on the dynamic column. Here $customer_details[$dyncust_field['attribute_name']] == 'cname' I want cell to be red otherwise it shows green. How to do this?. Can anyone help me please...

<?php
    foreach($customer as $customer_details)
    {?>
        <tr id="customer_details_<?=$customer_details['id']?>">
            <? 
                foreach($dyncust_fields as $dyncust_field)
                {
                    if($dyncust_field['add_to_listing']=='1')
                    {
                        echo "<td style='".$customer_details[$dyncust_field['attribute_name']] == 'cname' ?'color:red':'color:green'."'>".$customer_details[$dyncust_field['attribute_name']]."</td>";
                    }
                }
            ?>
        </tr>
    <? }
?>

or you can define classes as inline style or external styles

<style>
  .text-red{
    color:red;
  }
  .text-green{
    color:green;
  }
</style>

<?php
    foreach($customer as $customer_details)
    {?>
        <tr id="customer_details_<?=$customer_details['id']?>">
            <? 
                foreach($dyncust_fields as $dyncust_field)
                {
                    if($dyncust_field['add_to_listing']=='1')
                    {
                       $styleClass = $customer_details[$dyncust_field['attribute_name']] == 'cname' ?'text-red':'text-green'
                       echo "<td class='$styleClass'>".$customer_details[$dyncust_field['attribute_name']]."</td>";
                    }
                }
            ?>
        </tr>
    <? }
?>
<?php
    foreach($customer as $customer_details)
    {?>
        <tr id="customer_details_<?=$customer_details['id']?>">
            <? 
                foreach($dyncust_fields as $dyncust_field)
                {
                    if($dyncust_field['add_to_listing']=='1')
                    {
                       $color = $customer_details[$dyncust_field['attribute_name']] == 'cname' ?'red':'green';
                       $search = array("{{color}}","{{data}}");
                       $replace = array($color,$customer_details[$dyncust_field['attribute_name']] );
                       $template =  "<td style='color:{{color}};'>{{data}}</td>";
                       echo str_replace($search,$replace,$template);
                    }
                }
            ?>
        </tr>
    <? }
?>

Here $template is the template for a table cell.Values in $search array is replaced with the values of $replace array. In this way, you only need to customise the template,search and replace arrays. For example, following is the answer to your question, asked in the comment section.

$template = "<td> <a href='#list-corp-client' class='view-asset-inbox-model m-r-5 text-info' data-from='corporate' data-id='{{id}}' data-pk='1' data-toggle='modal'>{{title}}</a> </td>";
$search = array("{{id}}","{{title}}");
$replace = array($customer_details['id'], $asset_details['title']);
echo str_replace($search,$replace,$template);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM