我在从查询结果中提取数据时遇到问题。我的模型中有以下方法返回多行的结果
function method(){
$this->db->select('*');
$this->db->from('table_name');
$query = $this->db->get();
return $query->result();
}
从我的控制器我调用上面这个函数
$data['returned_records']= $this->model_name->method();
现在我想在视图中以表格格式显示returned_records。我该怎么做?
答案 0 :(得分:2)
<table>
<tr>
<th>Column Name</th>
</tr>
<?php foreach($returned_records as $row){ ?>
<tr>
<td><?php $row->column_name; ?></td>
</tr>
<?php } ?>
</table>
答案 1 :(得分:1)
以tablur形式显示视图中的输出代码如下:
<table border="1">
<tbody>
<tr>
<td>Heading 1</td>
<td>Heading 2</td>
// add more according to requirement
</tr>
<?php
foreach ($returned_records->result() as $row)
{
?><tr>
<td><?php echo $row->Column_name;?></td>
<td><?php echo $row->Column_name;?></td>
// add more according to requirement
</tr>
<?php }
?>
</tbody>
</table>
答案 2 :(得分:1)
<table border="1">
<tbody>
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
</tr>
<?php
foreach ($returned_records as $row)
{
?><tr>
<td><?php echo $row->Column_name1;?></td>
<td><?php echo $row->Column_name2;?></td>
</tr>
<?php }
?>
</tbody>
</table>
因为您已经返回&#34; query-&gt; result()&#34;无需再次写$ returned_records-&gt; result().. !!
答案 3 :(得分:0)
您必须在视图文件中以表格格式打印数据。
<table>
<th>Title</th>
<?php foreach($returned_records as $result){
<tr>
<td><?php echo $result->title;?></td>
</tr>
<?php
}
?>
</table>
答案 4 :(得分:0)
在你的控制器函数中传递$ data,如下面的代码:
$this->load->view('your_view',$data);
在您看来:
<table>
<tr>
<th>Column Name</th>
</tr>
<?php foreach($returned_records as $row){ ?>
<tr>
<td><?= $row->column_name ?></td>
</tr>
<?php } ?>
</table>