如何在PHP Codeigniter上制作动态表 [英] How to make dynamic table on php codeigniter

查看:43
本文介绍了如何在PHP Codeigniter上制作动态表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用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.

所以,通常情况下,我显示这样的表:

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>

但是当我使用3个字段或更多字段时,它的问题就无法解决,因此有任何建议

but the problem when i using 3 field or more field it can't be fit, so any suggestion for it?

推荐答案

您的问题:

您正在获取数据

并想在表中显示它,但不确定有多少列。

And want to display it in table but, not sure how many columns are there.

解决方案:

说,您有一个包含n条记录的多维数组。

Say, you have a multi-dimensional array with n records.

首先获取第一个元素(这是一个数据库行,一个表行)

First get the first element (which is a database row, a table row)

获取计数。

现在在数组。

使用foreach()语言构造。

Use foreach() language construct.

它将处理所有事情。

注意:此解决方案假定单个数组(数据库记录)具有相同的列数。

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

这篇关于如何在PHP Codeigniter上制作动态表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆