How to get users list in codeigniter?

by mallory_cormier , in category: PHP Frameworks , 3 months ago

How to get users list in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kadin , 3 months ago

@mallory_cormier 

To get a list of users in CodeIgniter, you can follow these steps:

  1. Create a model to interact with the database and retrieve the user data. For example, create a model called User_model.php in the models directory.
1
2
3
4
5
6
7
8
<?php
class User_model extends CI_Model {

    public function get_users() {
        $query = $this->db->get('users');
        return $query->result();
    }
}


  1. In your controller, load the User_model and call the get_users() method to retrieve the user data. For example, create a controller called User.php in the controllers directory.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
class User extends CI_Controller {

    public function index() {
        $this->load->model('User_model');
        $data['users'] = $this->User_model->get_users();
        
        $this->load->view('users_list', $data);
    }
}


  1. Create a view file to display the list of users. For example, create a view file called users_list.php in the views directory.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
<head>
    <title>Users List</title>
</head>
<body>
    <h1>Users List</h1>
    
    <ul>
        <?php foreach ($users as $user): ?>
            <li><?php echo $user->name; ?></li>
        <?php endforeach; ?>
    </ul>
</body>
</html>


  1. Access the controller method through the browser to see the list of users. For example, if your CodeIgniter application is running at http://localhost/your_app/, you can access the users list at http://localhost/your_app/index.php/user.


This is a simple example of how to get a list of users in CodeIgniter. You may need to modify the model, controller, and view based on your application requirements and database structure.