Welcome Guest, Not a member yet? Register   Sign In
Controller (from model to view)
#1

Hello, i'm new in CI , and i don't understand something.I have  a model like

PHP Code:
class Contact_model extends CI_Model {
 
 public function start()
 
 {
 
    $query $this->db->get('utilizatori'10);
 
    return $query->result();
 
 }



A controller 
PHP Code:
    class Contact extends CI_Controller{
        public function 
index(){
            
$this->load->model('Contact_model');
            
$data $this->Contact_model->start();
//here i added for testing a var_dump($data) and show me results from mysql successfully
            
$this->load->view('contact',$data);
        }
    } 

And i don't know in view (contact.php) how i can foreach($data) from model.How pass something like this in good practice?Thank u.
Reply
#2

Data passed to a view must be in an array. The key to the array is the name of the variable in the view. For instance, in the following controller method "contacts" is the key in the $data array that holds the model's return.

PHP Code:
class Contact extends CI_Controller{
 
    public function index()
 
    {
 
         $this->load->model('Contact_model');
 
         $data['contacts'] = $this->Contact_model->start(); 
 
         $this->load->view('contact',$data);
 
    }
 
   

The view would use the variable $contacts which is an array of objects. I have made up the contents of the object.
PHP Code:
foreach($contacts as $contact)
{
 
   echo $contact->name."<br>";
 
   echo $contact->address."<br>";
 
   echo $contact->phone."<br>";
 
   echo $contact->email."<br>";

Reply




Theme © iAndrew 2016 - Forum software by © MyBB