Codeigniter: display query result in controller

I am trying to display the result of my db request in my controller, but I do not know how to do this. could you show me?

controller

function get_name($id){ $this->load->model('mod_names'); $data['records']=$this->mod_names->profile($id); // I want to display the the query result here // like this: echo $row ['full_name']; } 

My model

 function profile($id) { $this->db->select('*'); $this->db->from('names'); $this->db->where('id', $id); $query = $this->db->get(); if ($query->num_rows() > 0) { return $query->row_array(); } else {return NULL;} } 
+4
source share
3 answers
 echo '<pre>'; print_r($data['records']); 

or

  echo $data['records'][0]['fullname']; 
+6
source

Model:

 function profile($id){ return $this->db-> select('*')-> from('names')-> where('id', $id)-> get()->row_array(); } 

Controller:

 function get_name($id){ $this->load->model('mod_names'); $data['records']=$this->mod_names->profile($id); print_r($data['records']); //All echo $data['records']['full_name']; // Field name full_name } 
+4
source

You do it inside a representation like this.

Controller:

  function get_name($id){ $this->load->model('mod_names'); $data['records']=$this->mod_names->profile($id); $this->load->view('mod_names_view', $data); // load the view with the $data variable } 

View (mod_names_view):

  <?php foreach($records->result() as $record): ?> <?php echo $record->full_name); ?> <?php endforeach; ?> 

I would change your model then to something like this (this worked for me):

 function profile($id) { $this->db->select('*'); $this->db->from('names'); $this->db->where('id', $id); $query = $this->db->get(); if ($query->num_rows() > 0) { return $query; // just return $query } } 
+3
source

Source: https://habr.com/ru/post/1413513/


All Articles