dongxing8766 2016-07-27 22:11
浏览 37
已采纳

如何为从php数组创建的html表中的不同行设置不同的样式?

I'm struggling with creating a html table from php array, while applying different css classes on different rows according to the array.

I have this code in data.php:

<?php
$data = array(
  array('Title 1'=>'text11', 'Title 2'=>'text12'),
  array('Title 1'=>'text21', 'Title 2'=>'text22'),
  array('Title 1'=>'text31', 'Title 2'=>'text32'),
  array('Title 1'=>'text41', 'Title 2'=>'text42', 'special'=>'style1'),
  array('Title 1'=>'text51', 'Title 2'=>'text52', 'special'=>'style2'),
  );
?>

I want to create a html table from this array, and if the array contains 'special'=>'style', it would set that style to that particular row. This is my code so far:

<?php include('data.php'); ?>
<table>
  <thead>
    <tr>
      <th>Title 1</th>
      <th>Title 2</th>
    </tr>
  </thead>
  <tbody>
<?php foreach ($data as $key=>$row):
        if ($row == 'class1') {
          $class='class="style1"';
          } elseif ($row == 'class1') {
            $class='class="style2"';
          } else {
            $class='';
          }?>
    <tr <?php echo $class ?>>
      <td><?php echo implode('</td><td>', $row); ?></td>
    </tr>
<?php endforeach; ?>
  </tbody>
</table>

And this is the desired output:

<table>
  <thead>
    <tr>
      <th>Title 1</th>
      <th>Title 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>text11</td><td>text12</td>
    </tr>
    <tr>
      <td>text21</td><td>text22</td>
    </tr>
    <tr>
      <td>text31</td><td>text32</td>
    </tr>
    <tr class="style1">
      <td>text41</td><td>text42</td>
    </tr>
    <tr class="style2">
      <td>text51</td><td>text52</td>
    </tr>
  </tbody>
</table>
  • 写回答

3条回答 默认 最新

  • dpy33121 2016-07-27 22:34
    关注

    Your problem is this line:

    <?php foreach ($data as $key=>$row):
    

    You're forgetting that you're looping over a multidimensional array (i.e. an array of arrays) so that $row is an array here. On the next line:

    if ($row == 'class1')
    

    You're looking for a comparison between a string and $row which is an array. This will never work! As Daniel points out in his answer, you need to look at the contents of the array.

    Don't forget you'll need to remove the special element from the array before displaying it. Personally, I'd condense the code a bit, though mixing PHP and HTML like this is never a good idea.

    <?php include('data.php'); ?>
    <table>
      <thead>
        <tr>
          <th>Title 1</th>
          <th>Title 2</th>
        </tr>
      </thead>
      <tbody>
    <?php foreach ($data as $row): $class = $row["special"] ?? ""; unset($row["special"]);?>
        <tr class="<?=$class?>">
          <td><?=implode("</td><td>", $row)?></td>
        </tr>
    <?php endforeach; ?>
      </tbody>
    </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料