【问题标题】:How to add CSS to the foreach result in CodeIgniter view?如何在 CodeIgniter 视图中将 CSS 添加到 foreach 结果中?
【发布时间】:2017-12-26 23:20:11
【问题描述】:

在这里,我使用 foreach 从表格购物车中获取了一些数据。现在我想显示如下(标题和值如表中所示):

控制器

   $query = $this->db->query('SELECT id,username,useremail FROM tbl_cart');

   $resultdata['results'] = $query->result_array(); 

$this->load->view('one/home_comman_page/head');
$this->load->view('one/usercart', $resultdata);
$this->load->view('one/home_comman_page/footer');
$this->load->view('one/home_comman_page/script');
}else{

        redirect('users/login');
    }
}    

查看

<?php

 foreach($results as $result)
 {

echo '<span>'.$result['id'].'</span>';echo'</br>';
echo '<span>'.$result['useremail'].'</span>';echo'</br>';
echo '<span>'.$result['username'].'</span>';echo'</br>';


}

如何像 db 表一样显示 foreach 结果数据?

【问题讨论】:

  • 您正在使用&lt;span&gt; 标签,它们是内联元素。这意味着他们将在页面上彼此相邻。您可能希望使用&lt;td&gt; 标签交换到表结构:)

标签: php html css codeigniter foreach


【解决方案1】:
 <table style="width:100%">
  <tr>
    <th>ID</th>
    <th>User Name</th>
    <th>User Email</th>
   </tr>
 <?php foreach($results as $result) {?>
  <tr>
    <td><?php echo $result['id']; ?></td>
    <td><?php echo $result['username']; ?></td>
    <td><?php echo $result['useremail']; ?></td>
  </tr>
 <?php } ?>

您也可以添加一些样式,例如:

<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
    text-align: left;    
}
</style> 

【讨论】:

    【解决方案2】:

    您应该使用 HTML 表格标签。

    <table>
      <?php foreach($results as $result)
      {
        echo '<tr style="border:1px solid grey;">';
        echo '<td>.$result['id'].</td>';
        echo '<td>.$result['useremail'].</td>';
        echo '<td>.$result['username'].</td></tr>';
    
      } ?>
    
    </table>
    

    【讨论】:

    • tr 应该在for 循环内,否则所有数据将显示在一行中,即一行tr
    【解决方案3】:

    第一个:您需要将记录集嵌入到 html table 中,如下所示

    第二个:在这里阅读基本的html表格标签refer here

    <table border="1px">
    <thead>
    <tr>
        <th>S.no</th>
        <th>ID</th>
        <th>User ID</th>
        <th>UserName</th>
        .......
    </tr>
    </thead>
    <tbody>
    <?php
    foreach ($results as $result) { ?>
        <tr>
            <td><?php echo $i; ?></td>
            <td><?php echo $result['user_id']; ?></td>
            <td><?php echo $result['username']; ?></td>
            ......
        </tr>
    
        <?php
    }
    ?>
    </tbody>
     </table>
    

    【讨论】:

      【解决方案4】:
      echo "<table class='table_Class' >";
      foreach($results as $result)
      {
      echo "<tr>";
      echo "<td>".$result['id']."</td>";
      echo "<td>".$result['useremail']."</td>";
      echo "<td>".$result['username']."</td>";
      echo "</tr>";
      }
      echo "</table>";
      

      【讨论】:

      • 好收获。这个奖项肯定是给阿米拉的。谢谢。
      【解决方案5】:

      尝试使用表格,你做错了

      <table>
      <thead>
      <tr>
          <td>ID</td>
          <td>User Email</td>
          <td>Username</td>
      </tr>
      </thead>
      <tbody>
      <?php
      foreach ($results as $result) { ?>
          <tr>
              <td><?php echo $result['id']; ?></td>
              <td><?php echo $result['useremail']; ?></td>
              <td><?php echo $result['username']; ?></td>
          </tr>
      
          <?php
      }
      ?>
      </tbody>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多