我编写的以下代码仅显示表格中的最后一行。但是,我需要loop
并在表格中显示每个row
。
class Catalog_products extends CI_Model {
function Show_all_products()
{
$q = $this->db->get('printer');
foreach ($q->result() as $row)
{
$data = array();
$data['id'] = $row->id;
$data['name'] = $row->name;
}
return $data;
}
}
<?php echo $id ?>
<?php echo $name ?>
public function catalog()
{
$this->load->model('Catalog_products');
$data = $this->Catalog_products->Show_all_products();
$this->load->view('catalog', $data);
}
答案 0 :(得分:6)
型号:
function Show_all_products()
{
return $this->db->get('printer')->result();
}
控制器:
$data['products'] = $this->Catalog_products->Show_all_products();
$this->load->view('catalog', $data);
查看:
foreach($products as $product) {
echo $product->id;
echo $product->name;
}