-1

我想在视图页面中显示 2 行作为输出。再次,当我单击转到下一页时,它将显示 2 下一行,依此类推(我总共有 8 行在表中)。但是当我运行以下代码时,它会在视图页面中显示所有 8 行以及分页链接。我一整天都在寻找不工作的真正原因,但我的问题仍然没有解决。我在互联网上搜索了相关查询的帮助,但没有用。Finalyy我在这里用我自己的话来解释这个问题。我已经注释了代码中的几乎所有行。我正在自动加载中加载库以进行分页和表单和 URL 的帮助程序。如果有人帮助我,我将非常感激。提前致谢。

   <?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 的模型

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

最后这是我用来显示内容的视图页面。

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

1 回答 1

1

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

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

在模型中你需要限制结果

 <?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.
            }
        }
    ?>
于 2012-04-12T12:18:06.690 回答