1

我编写的以下代码仅显示表中的最后一行。但是我需要在表格中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);
}
4

1 回答 1

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;
}
于 2012-09-28T03:28:00.630 回答