【问题标题】:How to give a color to a perticular column data in codeigniter?如何为codeigniter中的特定列数据赋予颜色?
【发布时间】:2020-07-19 12:55:30
【问题描述】:

<?
	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' 我希望单元格为红色,否则显示为绿色。这个怎么做 ?。谁能帮帮我...

【问题讨论】:

  • 发布您的HTML

标签: php html css mysql codeigniter


【解决方案1】:
<?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>
    <? }
?>

这里$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);

【讨论】:

  • 最好将$template$search 排除在foreach 循环之外。我把它留在那里让你容易理解。
【解决方案2】:
<?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>
    <? }
?>

或者您可以将类定义为内联样式或外部样式

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

【讨论】:

猜你喜欢
  • 2021-09-15
  • 2013-10-04
  • 1970-01-01
  • 2015-05-18
  • 2020-05-17
  • 2020-10-19
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多