douweinu8562 2014-11-15 14:02
浏览 29
已采纳

具有条件文本颜色的CSV到html表

I'm trying to convert a csv to table with following code:

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

Now I want to add conditionality like:

if $line_of_text[2] <0 

add fontawesome icon fa fa-caret-down and text should be red

if $line_of_text[2] >0 

add fontawesome icon fa fa-caret-up and text should be green

How can I achieve this?

  • 写回答

1条回答 默认 最新

  • douhaodang0403 2014-11-15 14:29
    关注

    You can achieve this by creating a $style and a $icon variables, determine their values from $line_of_text[2] value, then applying this style to the tr's tds and adding the $icon content in front of the first td text. ($icon can be empty, ie if $line_of_text[2] equals 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?