Codeigniter MVC /显示表中的所有记录

时间:2012-09-28 03:23:46

标签: php codeigniter

我编写的以下代码仅显示表格中的最后一行。但是,我需要loop并在表格中显示每个row

MODEL

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;

    }

}

VIEW

<?php echo $id ?>
<?php echo $name ?>

CONTROLLER

public function catalog()
{

    $this->load->model('Catalog_products');
    $data = $this->Catalog_products->Show_all_products();
    $this->load->view('catalog', $data);
}

1 个答案:

答案 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;
}