John Doe
John Doe

Reputation: 29

Select and display all rows in Codeigniter

I'm novice in Codeigniter framework and I want to ask if is good or it exist another method to display all rows from database in view page.

I have this controller:

    class Dashboard extends CI_Controller{

            public function index()
    {

$data['user'] = $this->dashboard_model->get_user_details($this->session->userdata('logged_in'));  

            $this->load->view('includes/header', $data);

Model dashboard:

class Dashboard_model extends CI_Model{

    public function get_user_details($user_id){

        $this->db->select("*");
        $this->db->where('id', $user_id);
        $result = $this->db->get('users');
        return $result->result_array ();
    }
}

And I display in view page like:

<?php echo $user[0]['id']; ?> <?php echo $user[0]['username']; ?>

Code is working, it show me what I want but I don't know if this is a good solution.

Upvotes: 0

Views: 5818

Answers (5)

Danish Ali
Danish Ali

Reputation: 2352

In your MODEL

class Dashboard_model extends CI_Model{
public function get_user_details($user_id){

return this->db->get_where('users', array('id' => $user_id))->result_array(); //You can use result_array() for more than one row and row_array() only for one row
//If you want to show all rows form database use this return $this->db->get('users')->result(); OR result_array()
}

}

In your View

For more than one row use foreach loop

foreach($user as $usr){
echo $usr['']; //Add all your database data here like this
}

Upvotes: 0

user4419336
user4419336

Reputation:

For single user information I would use row_array() or row()

https://www.codeigniter.com/userguide3/database/queries.html

<?php

class Dashboard_model extends CI_Model{

    public function get_user_details($user_id){

        $this->db->where('id', $user_id);
        $result = $this->db->get('users');
        return $result->row_array();
    }
}

Controller

<?php

class Dashboard extends CI_Controller {

    public function index()
    {

        $this->load->model('dashboard_model');

        // Use the users id stored in your session when logged in 
        $userinfo = $this->dashboard_model->get_user_details($this->session->userdata('id'));  

        $data['id'] = $userinfo['id']; 

        $data['username'] = $userinfo['username']; 

        $this->load->view('includes/header', $data);

        }

}

Then echo on view

<?php echo $username;?> example <?php echo $id;?>

Upvotes: 0

Alex
Alex

Reputation: 9265

By applying $this->db->where('id', $user_id); you will only get results (1) for the user with $user_id (not all users) and (2) you will only get results if a user with that id exists in the database. The correct way to get all users, while slightly modifying your function to support returning only one user is as follows:

/**
 * @param int $user_id When NULL will return all users
 * @return array
 */
public function get_user_details($user_id = null) {
    $this->db->from('users');
    if (!is_null($user_id)) {
        $this->db->where('id', $user_id);
        $q = $this->db->get();
        if ($q->num_rows() !== 1) {
            return array();
        }
        return $q->row_array();
    } else {
        return $this->db->get()->result_array();
    }
}

So to get all users: $this->dashboard_model->get_user_details()

To get logged in user: $this->dashboard_model->get_user_details($this->session->userdata('logged_in'))

To get a user with id 123: $this->dashboard_model->get_user_details('123')

When $user_id is blank you can go through results like:

if (count($users) !== 0) {
    foreach ($users as $user) {
        echo $user['id'];
        echo $user['username'];
    }
} else {
    echo 'No users';
}

When $user_id is set you get a single result thus this will work:

if (count($users) !== 0) {
    echo $users['id'];
    echo $users['username'];
} else {
    echo 'User with that id does not exist!';
}

Upvotes: 1

Touheed Khan
Touheed Khan

Reputation: 2151

Updated answer : As per details provided in commnets

instead of return $result->result_array(); use return $result->row_array();

View :

<?php echo $user['id']; ?>
<?php echo $user['username']; ?>

For fetching multiple rows :

return $result->result_array(); will give you array of all the users present in users table.

What you need to do is, access those users in view page using foreach loop.

<?php   
foreach ($user as $row) {
    echo $row['id'];
    echo $row['username']; 
}
?>

Upvotes: 0

prakash tank
prakash tank

Reputation: 1267

You can also use return $result->row_array (); if it returns only single row.

and then in your view file get the data by using : $user['id'];

for multiple rows :

your solution is fine but you need to add foreach loop and get the data

ex:

foreach($user as $usr) {
  echo $usr['id']; echo  "<br/>";
  echo $usr['username'];
}

Upvotes: 1

Related Questions