Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display query in view codeigniter

Tags:

codeigniter

lets say the query from my controller is $query.
how to display the data other than using this kind of code?

foreach($query as $row){
echo $row->student_id;
}

any other than this coz the query only have one row
any way that can i access the returned array like this?

$query['student_id'];
like image 880
Azuan Avatar asked Feb 12 '26 05:02

Azuan


1 Answers

let's say your table have the following fields

student_id and student_name

have your model or controller return only one row.

function get_first_student() {

    $query = this->db->select('*')->from('student')-where('student_id','1')->limit(1);
    $data['stuff'] =  $query->row_array(); // or $query->row() which returns an object
    $this->load->view('whatever',$data)

}

then in your view

<p>student name: <?= $stuff['student_name'] ?> </p>
<p>student id: <?= $stuff['student_id'] ?> </p>
like image 169
afarazit Avatar answered Feb 16 '26 17:02

afarazit