我尝试使用以下代码将csv转换为表格:
<?PHP
$file_handle = fopen("data/points.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
echo '<tr><td>' . $line_of_text[0] . '</td><td>' . $line_of_text[1] . '</td><td>' . $line_of_text[2] . '</td><td>' . $line_of_text[3] . '</td><td>' . $line_of_text[4] . '</td></tr>';
}
fclose($file_handle);
?>
</table>
现在我想添加条件:
if $line_of_text[2] <0
添加fontawesome图标fa fa-caret-down,文字应为红色
if $line_of_text[2] >0
添加fontawesome icon fa fa-caret-up,文字应为绿色
我怎样才能做到这一点?
答案 0 :(得分:0)
您可以通过创建$style
和$icon
变量,从$line_of_text[2]
值确定其值,然后将此样式应用于tr
&#39;来实现此目的。 s td
s并在第一个$icon
文字前添加td
个内容。 ($icon
可以为空,即如果$line_of_text[2]
等于0):
<?PHP
$file_handle = fopen("data/points.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
$style = '';
$icon = '';
if ($line_of_text[2] < 0){
$style = ' style="color:red;"';
$icon = '<i class="fa fa-caret-down"></i> '; //icon fa fa-caret-down
}
if ($line_of_text[2] > 0){
$style = ' style="color:green;"';
$icon = '<i class="fa fa-caret-up"></i> '; //icon fa fa-caret-down
}
echo '<tr><td' . $style . '>' . $icon . $line_of_text[0] . '</td><td>' . $line_of_text[1] . '</td><td'.$style.'>' . $line_of_text[2] . '</td><td'.$style.'>' . $line_of_text[3] . '</td><td>' . $line_of_text[4] . '</td></tr>';
}
fclose($file_handle);
?>
</table>