CodeIgniter Forums

Full Version: how to show object data in view file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]ludo31[/eluser]
Hello ;

I would like to show in view files the resultat of this query result

in model (here I would like to show the last line of inserting in the table
Code:
public function getId()
        {
            $this->db->select('id','nom');
            $this->db->from('chaussure');
           $this->db->order_by("id", "desc");
            $this->db->limit(1);
            $query = $this->db->get();
            
              if($query->num_rows()>0)
                       {
                         foreach($query->result()as $row)
                         {
                           $data[] = $row;
                         }
                            
                        
                          return $data ;
                       }
        }

in controller

Code:
public function envoyer_chaussure()
    {
  $identifiant[] =  $this->site_model->getId();
              
              $this->load->view('details',$identifiant);


}

in view file

Code:
<body>
    <h1>Details sur les Chassure</h1>
    
  
        &lt;?php  if($identifiant!=null):
            foreach ($identifiant as $item): ?&gt;
            
             &lt;?php echo $item->nom ; ?&gt;
            
              &lt;?php echo $item->id ; ?&gt;
            
             &lt;?php endforeach; endif; ?&gt;

I have Message: Undefined variable: identifiant

thanks !! did you know the solution !! I know here it's an object so I use $item->nom

El Forum

[eluser]CroNiX[/eluser]
Since you are only retrieving 1 row (limit 1) in your getId() method, use $row instead of $result. I'm not sure why you are looping over your results and putting them in a new array. It's already an array of objects (or array of arrays if returning data as array). Just return $query->row(); See the manual for generating query results for the explanations.

Then, in your envoyer_chaussure() method, you need to do something like:
Code:
$data['identifiant'] = $this->site_model->getId();  //the array key "identifiant" will now be $identifiant in your view
$this->load->view('details', $data);

Your view should work then.