如何在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'我希望单元格是红色的,否则它显示绿色。如何做到这一点?。有人能帮我吗。。。

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

相关内容

  • 没有找到相关文章

最新更新