简体   繁体   English

如何在PHP Codeigniter上制作动态表

[英]How to make dynamic table on php codeigniter

i'm new on using codeigniter, i want to ask how to make dynamic table so when i select data from anytable form database it can be fit with the table, even the field is different. 我是使用Codeigniter的新手,我想问一下如何制作动态表,因此当我从anytable表单数据库中选择数据时,它可以适合表,即使字段不同。

so, in normally i show table like this : 因此,通常我显示这样的表:

<table class="table table-striped">
    <thead>
      <tr>
        <th scope="col">#Number</th>
        <th scope="col">Field</th>
      </tr>
    </thead>
    <tbody>
      <?php
      $no = 1;
       foreach ($data as $row) {?>
          <tr>
            <th scope="row"><?php echo $no++?></th>
            <td><?php echo $row->COLUMN_NAME ?></td>
          </tr>
      <?php } ?>
    </tbody>
</table>

but the problem when i using 3 field or more field it can't be fit, so any suggestion for it? 但是,当我使用3字段或更多字段时,问题就无法解决,因此对此有何建议?

Your problem: 你的问题:

You are fetching data from database. 您正在从数据库中获取数据。

And want to display it in table but, not sure how many columns are there. 并想在表中显示它,但不确定有多少列。

Solution: 解:

Say, you have a multi-dimensional array with n records. 假设您有一个包含n条记录的多维数组。

First get the first element (which is a database row, a table row) 首先获取第一个元素(这是数据库行,表行)

Get count of it. 得到它。

Now loop over the array. 现在循环遍历数组。

Use foreach() language construct. 使用foreach()语言构造。

It will take care of every thing. 它将处理所有事情。

Note: This solution assumes that the individual array (database records) are having same number of columns. 注意:此解决方案假定各个数组(数据库记录)的列数相同。

<?php
if (! empty($arr)) {
  foreach ($arr as $elem) {
?>
    <tr>
<?php
   if (! emtpy($elem)) {
     foreach($elem as $td) {
?>
     <td><?php echo $td;?></td>
<?
     }
   }
   </tr>
<?   
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM