分页在codeigniter中不起作用:( [英] pagination is not working in codeigniter :(

查看:96
本文介绍了分页在codeigniter中不起作用:(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在视图页面中显示2行作为输出.同样,当我单击以转到下一页时,它将显示接下来的2行,依此类推(总共共有8行) 桌子).但是,当我运行以下代码时,它将在视图页面中显示所有8行以及分页链接.我整天试图找到不这样做的实际原因 工作,但我的问题仍然没有解决.我在互联网上搜索相关查询的帮助,但没有用.最后,我在这里用我自己的话来解释 问题.我已经在代码中注释了几乎所有行.我正在自动加载库以进行分页,并在窗体和URL的帮助器中进行加载.如果真的我会很感激 有人帮我.预先感谢.

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.
                }
            }
        }
        ?>

以下代码来自于我的名为User

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(); ?>

推荐答案

您正在从模型中获取所有用户.那不是你做的方式. 您的控制器应该是这样的:

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.
            }
        }
    ?>

这篇关于分页在codeigniter中不起作用:(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆