如何为 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>

    <? }

?>

这里我写了一些代码来显示动态列的数据,这里我想给特定列的特定数据一个颜色。但它不工作。这里这一行$customer_details[$dyncust_field['attribute_name']]是用来根据动态列获取表记录的。在这里,$customer_details[$dyncust_field['attribute_name']] == 'cname'我希望单元格为红色,否则显示绿色。这个怎么做 ?。谁能帮帮我...



蛊毒传说
浏览 83回答 2
2回答

郎朗坤

<?php&nbsp; &nbsp; foreach($customer as $customer_details)&nbsp; &nbsp; {?>&nbsp; &nbsp; &nbsp; &nbsp; <tr id="customer_details_<?=$customer_details['id']?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($dyncust_fields as $dyncust_field)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($dyncust_field['add_to_listing']=='1')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$color = $customer_details[$dyncust_field['attribute_name']] == 'cname' ?'red':'green';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$search = array("{{color}}","{{data}}");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$replace = array($color,$customer_details[$dyncust_field['attribute_name']] );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$template =&nbsp; "<td style='color:{{color}};'>{{data}}</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo str_replace($search,$replace,$template);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; <? }?>这$template是表格单元格的模板。$search数组中的值替换为$replace数组的值。这样,你只需要自定义模板,搜索和替换数组。例如,以下是您在评论部分提出的问题的答案。$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);

喵喵时光机

<?php&nbsp; &nbsp; foreach($customer as $customer_details)&nbsp; &nbsp; {?>&nbsp; &nbsp; &nbsp; &nbsp; <tr id="customer_details_<?=$customer_details['id']?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($dyncust_fields as $dyncust_field)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($dyncust_field['add_to_listing']=='1')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td style='".$customer_details[$dyncust_field['attribute_name']] == 'cname' ?'color:red':'color:green'."'>".$customer_details[$dyncust_field['attribute_name']]."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; <? }?>或者您可以将类定义为内联样式或外部样式<style>&nbsp; .text-red{&nbsp; &nbsp; color:red;&nbsp; }&nbsp; .text-green{&nbsp; &nbsp; color:green;&nbsp; }</style><?php&nbsp; &nbsp; foreach($customer as $customer_details)&nbsp; &nbsp; {?>&nbsp; &nbsp; &nbsp; &nbsp; <tr id="customer_details_<?=$customer_details['id']?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($dyncust_fields as $dyncust_field)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($dyncust_field['add_to_listing']=='1')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$styleClass = $customer_details[$dyncust_field['attribute_name']] == 'cname' ?'text-red':'text-green'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "<td class='$styleClass'>".$customer_details[$dyncust_field['attribute_name']]."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; <? }?>
打开App,查看更多内容
随时随地看视频慕课网APP