Show request in codeigniter view

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

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

any other than that, the query has only one row,
how can I access a returned array like this?

$query['student_id'];
+3
source share
3 answers

let's say your table has the following fields

student_id and student_name

If your model or controller returns 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>
+2
source

Actually, it $query->row()simply selects the first row.

So, instead of iterating over the result, you can simply do:

$student_id = $query->row()->student_id; // (untested)

OR

$row = $query->row();
$student_id = $row->student_id;

Assuming the query will always return a string.

+1

, . . :

CONTROLLER:

$this->load->model('Profile');
$data['row'] = $this->Profile_model->profile_read(); //get profile data
$this->load->view('profile_view', $data); //load data to view

Model:

function profile_read()
{
        $this->db->where('user_id', $user_id);
        $query = $this->db->get('user_profiles'); //get all data from user_profiles table that belong to the respective user
        return $query->row(); //return the data    
}

In the model, you can use all other database functions (create, delete, update)

VIEW:

<?php echo $row->name; ?>
<?php echo $row->email; ?>
<?php echo $row->about; ?>
etc

Finally, in the view, you can echo from any rows from the table owned by the user.

0
source

All Articles