pagination is not working in codeigniter :(

2019-09-20 04:27发布

问题:

I want to display 2 rows in view page as an output. Again when i click on to go to the next page it will display 2 next rows and so on ( All together I have 8 rows in table). But when I run the following code it displays all 8 rows in the view-page along with pagination links. I tried whole day to find the actual reason for not working it but my problem is still unsolved. I searched for the help on internet with related query but it was of no use. Finalyy I am here with my own words to explain the problem. I've commented almost all the lines in my code. I am loading libraries in autoload for pagination and helper for form and url. I'll really be thankful if somebody help me out. Thanks in advance.

   <?php
        class ManageUser extends CI_Controller {  // creating class for the controller
            function index()
            {
                if($this->session->userdata('logged_in'))  // checking users under session if he is already logged in
                {           
                    $this->load->model('admin/user');     // loading model . I am not using Datamapper.
                    $result = $this->user->view_user();   // getting response from the model and storing it to result
                    $total_rows = count($result);         // counting number of rows countered ( its 8 in in my database ) 
                    //echo $total_rows;


                    if($result)
                    {
                        $data['users'] = $result;
                        $config['base_url'] = "http://192.168.0.102/project/index.php/admin/manageUser";  // this is the address where I am pointing the view page url
                        $config['total_rows'] = $total_rows;        // Total numbers of rows assigned to pagination-config
                        $config['per_page'] = '2';                  // I want to display 2 rows in 1 page
                        $config['uri_segment'] = '2';
                        $this->pagination->initialize($config);     // initilizing the pagination-config
                        //$data['pagination'] = $this->pagination->create_links();  
                        //$this->load->vars($data);
                        $this->load->view('admin/manageUser',$data);  // Loading the page
                    }

                    //$this->load->view('admin/manageUser',$data);
                }
                else
                {
                    redirect('admin/login');    // incase of faliured session user will be redirected to the login-page.
                }
            }
        }
        ?>

Following code is from my model named User

 <?php
            Class User extends CI_Model   // extending the model
            {
                function __construct()
                {
                    parent::__construct();
                }

                function view_user()        // function which is loaded from controller
                {

                    $query = $this -> db -> get('users');  //query to fetch all the information from the database
                    return $query->result();        // returning result to the Controller.
                }
            }
        ?>

And finally this is the view page I am using to display the content .

  <!-- This is the view page -->

        <?php if(isset($users)) { ?>     <!-- Checking if user is set -->
             <?php foreach($users as $user) { ?>  <!-- running in a loop to accept all the value from the database and display it row wise -->
                <td><?php echo $user -> us_display_name; ?></td>   <!-- Displaying name -->
                <td><?php echo $user -> us_first_name . " " . $user -> us_last_name; ?></td> <!-- Displaying first name and last name together -->
                <td><?php echo $user -> us_email_id; ?></td>  <!-- Displaying email-ID -->
        <?php } } else { ?>
        <tr>    <td>No records found!</td>
        <?php } ?>
        <?php echo $this->pagination->create_links(); ?>

回答1:

You are getting all the users from the Model. That's not how you do it. Your controller should be something like this :

<?php
    class ManageUser extends CI_Controller {  // creating class for the controller
        function index($offset)
        {
            if($this->session->userdata('logged_in'))  // checking users under session if he is already logged in
            {           
                $this->load->model('admin/user');     // loading model . I am not using Datamapper.
                $perpage = 2;

                $result = $this->user->view_user($offset,$perpage);   // getting response from the model and storing it to result
                $total_rows = $this->db->count_all('users');


                if($result)
                {
                    $data['users'] = $result;
                    $config['base_url'] = "http://192.168.0.102/project/index.php/admin/manageUser";  // this is the address where I am pointing the view page url
                    $config['total_rows'] = $total_rows;        // Total numbers of rows assigned to pagination-config
                    $config['per_page'] = $perpage;                  // I want to display 2 rows in 1 page
                    $config['uri_segment'] = '2';
                    $this->pagination->initialize($config);     // initilizing the pagination-config
                    //$data['pagination'] = $this->pagination->create_links();  
                    //$this->load->vars($data);
                    $this->load->view('admin/manageUser',$data);  // Loading the page
                }

                //$this->load->view('admin/manageUser',$data);
            }
            else
            {
                redirect('admin/login');    // incase of faliured session user will be redirected to the login-page.
            }
        }
    }
    ?>

And in the model you need to limit the result

 <?php
        Class User extends CI_Model   // extending the model
        {
            function __construct()
            {
                parent::__construct();
            }

            function view_user($offset,$limit)        // function which is loaded from controller
            {

                $query = $this -> db -> get('users',$perpage,$offset);  //You dont fetch all the data from the database
                return $query->result();        // returning result to the Controller.
            }
        }
    ?>