Codeigniter:在控制器中显示查询结果

时间:2012-05-21 11:26:14

标签: codeigniter codeigniter-2

我试图在我的控制器中显示我的数据库查询结果,但我不知道该怎么做。你能告诉我吗?

控制器

 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'];

 }

我的模特

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;}

    }   

3 个答案:

答案 0 :(得分:6)

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

 echo $data['records'][0]['fullname'];

答案 1 :(得分:4)

型号:

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

控制器:

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

}

答案 2 :(得分:3)

你在视图中这样做,就像这样。

控制器:

 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

 }

查看(mod_names_view):

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

我会修改你的模型然后改为这样的东西(它对我有用):

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
    }
}
相关问题