4

I am trying to display my db query result in my controller, but I don't know how to do it. could you please 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;}

    }   
black_belt
  • 6,601
  • 36
  • 121
  • 185

3 Answers3

6
echo '<pre>';
print_r($data['records']);

or

 echo $data['records'][0]['fullname'];
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
4

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

}
James Arnold
  • 990
  • 1
  • 6
  • 15
3

You do that inside a View, 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 modify your model then to something like this (it 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
    }
}
ionize
  • 101
  • 1
  • 8