CodeIgniter, pass data from model to controller to use in view

31,893

Solution 1

I think you made 2 mistakes

  1. forget to fetch the result to an array
  2. forget to pass the data to view

change in your model class.

public function get_users(){

    $data = array();
    $query = $this->db->get('users');
    $res   = $query->result();        
    return $res;

 }

change in your controller class

public function index() {

    $this->load->view('template/header');

    $this->load->model('manage_accounts_model');

    $this->load->view('template/footer');

    $this->load->model('manage_accounts_model');

    $res = $this->manage_accounts_model->get_users();

    if($res){
        $data['result'] = $res;
        $this->load->view('manage_accounts_view', $data);

    } else {

        echo "Fail";

    }

in your view

  print_r($result);

Solution 2

cause $data is not defined in your controller try

$data = array();
if($res){
  $data['res'] = $res;
  $this->load->view('manage_accounts_view', $data);
}

Then get on view

<?php var_dump($res); ?>

Also you are sending whole query from model not result for return result you need like :-

public function get_users(){
 $query = $this->db->get('users');
 return $query->result();
}
Share:
31,893
Richard
Author by

Richard

Updated on October 30, 2020

Comments

  • Richard
    Richard over 3 years

    I want to pass data queried in my model to the controller, to do so I am using return $data. Then in the controller I use $this->load->view('my_view', $data);

    From my understanding var_dump($data); in the view should show me the results from the query... This is not the case. I am getting "undefined variable data" and NULL from the var_dump($data);.

    Here is my model:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Manage_accounts_model extends CI_Model {
    
    
        public function index() {
    
            //
    
        }
    
        public function get_users(){
    
            $data = array();
    
            $data['query'] = $this->db->get('users');
    
            return $data['query'];
    
         }
    
    }
    

    Here is my controller

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Manage_accounts extends CI_Controller {
    
        public function index() {
    
            $this->load->view('template/header');
    
            $this->load->model('manage_accounts_model');
    
            $this->load->view('template/footer');
    
            $this->load->model('manage_accounts_model');
    
            $res = $this->manage_accounts_model->get_users();
    
            if($res){
    
                $this->load->view('manage_accounts_view', $data);
    
            } else {
    
                echo "Fail";
    
            }
    
      }
    
    }
    

    And finally my view:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    ?>
    <div class="container">
    
        <h1><?php if($title){ echo $title; } ?></h1>
    
        <?php var_dump($data['query']); ?>
    
    </div>
    
  • Tintu C Raju
    Tintu C Raju about 9 years
    $data['result'] = $res; $this->load->view('manage_accounts_view', $data); so $result can be accessible in view. $data is an associative array and its key can be used as a variable in view. sorry if iam wrong.
  • jogesh_pi
    jogesh_pi about 9 years
    $result holds the array elements and using echo for array only print Array(), This is what i mean only. You should use print_r($result) or var_dump($result) to highlight the result.