Search code examples
codeignitercodeigniter-2codeigniter-3

how to get values from model and display in controller


I am making a site in codeigniter, here is my controller, model and view:

controller:

function checking()
 {
 $email=$this->input->post('email');
 $this->load->model('login_model');
 $data['dbemail']=$this->login_model->email();// assign your value to CI variable  
 $this->load->view('home', $data); //passing your value to view
 }

model:

public function email()
 {
   $query = $this->db->query("SELECT email FROM change_password");
   $result = $query->result_array();
   return $result;
 }

view:

foreach ( $dbemail as $new_dbemail )
   {
      echo $new_dbemail['database_field'];//in here you can get your table header. 
     //Ex if your table has name field and you need to sho it you can use $new_dbemail['name'] 
    }

but i want to get $new_dbemail['name'] in my controller, how i can get the value of $new_dbemail['name'] in controller rather than view ???


Solution

  • To do so... you have to fetch data into a row_array not in result_array in Model.

    Controller

    function checking()
    {
    $email=$this->input->post('email');
    $this->load->model('login_model');
    $data['dbemail']=$this->login_model->email();  
    $database_field = $data['dbemail']['database_field']; //to get result in controller
    $date['database_field'] = $data['dbemail']['database_field']; //to get result in view
    $this->load->view('home', $data);
    }
    

    Model

    public function email()
    {
    $query = $this->db->query("SELECT email FROM change_password");
    return $query->row_array();   
    }
    

    View

    <p> Database Field value : <?=$database_field;?></p>