CodeIgniter Forums
Why does it return only one data? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Why does it return only one data? (/showthread.php?tid=25131)



Why does it return only one data? - El Forum - 12-02-2009

[eluser]shinokada[/eluser]
I have the following model, control and view.
It returns only one data.

Could anyone point out what I am doing wrong please?

Model

Code:
function getGalleryone(){
  
   $data = array();
  
     $Q = $this->db->get('products');
    
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data['products'] = $row;
       }
      }
    
    $Q->free_result();
    
    return $data;
}

Control

Code:
function galleryone(){
    $data['title'] = "Gallerie 1";
    $data['products'] = $this->MProducts->getGalleryone();
// getGalleryone returns id, name shortdesc thumbnail image class grouping category
    $data['main'] = 'galleryone';// this is using views/galleryone.php
    $this->load->vars($data);
    $this->load->view('template');
  }

View

Code:
foreach ($products as $key =>$product)
{
echo "<pre>";    
print_r ($product);
echo "</pre>";
}



Why does it return only one data? - El Forum - 12-02-2009

[eluser]rogierb[/eluser]
You are overwriting the same variable: $data['products']
Code:
$data['products'] = $row;

It should be
Code:
$data['products'][] = $row;

or
Code:
$data['products'][$row->id] = $row;



Why does it return only one data? - El Forum - 12-02-2009

[eluser]shinokada[/eluser]
Thanks rogierb.