使用php创建具有特定列数的表列

时间:2016-10-27 09:54:04

标签: php html codeigniter html-table

我有一个数据数组并希望在表上显示它,但是根据数组数据的数量出现在表中的列,如何使12列数组默认但数字仍保留在数据库中。假设表中有2个数据,但有多个列12 ......?

模型

function get_id($id) {
    $this->db->where('my_id', $id);
    $get_data = $this->db->get('mytable');
    if ($get_data->num_rows() > 0) {
        foreach ($get_data->result() as $data) {
            $my_result[] = $data;
        }
        return $my_result;
    }
}

控制器

public function myControllers() {
    $id =  $this->uri->segment(4);
    $data=array('detail'    => $this->mymodels->get_id($id));
    $this->load->view('layout/wrapper', $data);
}

浏览

<table>
    <thead> 
        <tr>
            <td>No</td>
            <td>ID</td>
            <td>Name</td>
            <td>Class</td>
        </tr>
    </thead>  
    <tbody> 
    <?php
        $no = 1;
        foreach ($detail as $row) {         
            echo '<tr>';
            echo '<td>'.$no.'</td>';
            echo '<td>'.$row->id.'</td>';
            echo '<td>'.$row->name.'</td>';
            echo '<td>'.$row->class.'</td>';
            echo '</tr>';

            $no++;
        }           
    ?>
    </tbody>      
</table>

我期待这个例子

NO  | ID  | NAME | ClASS |
____|____ |______|_______|
 1  | 001 | Paul |    x  |
 2  |     |      |       |
 3  |     |      |       |
 4  |     |      |       |
to 12

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你希望表中有12行的默认值,即使只有2行填充了数据。

如何在foreach循环后添加while循环,继续递增计数器并添加行直到计数器达到12?

while ($no <= 12) {
    echo '<tr>';
    echo '<td>'.$no.'</td>';
    echo '<td></td>';
    echo '<td></td>';
    echo '<td></td>';
    echo '</tr>';
    $no++;
}
相关问题