【问题标题】:Create table column with the number of a particular column with php使用php创建具有特定列号的表列
【发布时间】:2016-10-27 09:54:04
【问题描述】:

我有一个数据数组,想在表格中显示,但是表格中出现的列是根据数组数据的个数,如何让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

【问题讨论】:

  • 列还是行。?
  • @noufalcep 列和默认第 12 行,虽然只有四列和行数据有 12 个

标签: php html codeigniter html-table


【解决方案1】:

如果我理解您的问题,您希望表中有 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++;
}

【讨论】:

  • yes 更正它的默认行和第12列,虽然有2或5列数据仍然是12行,好的我试试她
  • 在我尝试添加脚本后foreach ($ data_pemeliharan as $ row) while ($ No. &lt;= 12) {没有第12列但数据相同,即使只有2个数据,其他10个列也应该没有数据跨度>
  • 所以所有 12 行都存在,但列中没有数字?在第一个 &lt;td&gt; 标记中添加 ' . $no . '。我已经编辑了我的原始答案以包含此内容。
  • 您的示例脚本生成具有相同值的ID Name class 12 列,我希望如果我有一个数据,固定列数 12 只有一个有数据出现在表中,另一个列空白列
猜你喜欢
  • 2021-06-13
  • 1970-01-01
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多