简体   繁体   English

使用Redis和PHP进行分页

[英]Pagination using Redis and PHP

I am working on a project in which i need to perform pagination on the records fetch. 我正在一个项目中,我需要对获取的记录进行分页。 Can anyone please help me to get the work done in an efficient way. 谁能帮助我高效地完成工作。

Please guide me which way would be best: 1) On Page load Fetch complete record and send it to the frontend and display the records as and when new page is requested. 请指导我哪种方法最好:1)在页面加载时获取完整的记录并将其发送到前端,并在请求新页面时显示记录。 (Here i feel this will consume lot of time and make the application heavy on userside) (在这里,我觉得这会浪费很多时间,并使应用程序对用户造成沉重负担)

2)ON Page load fetch the record from server and create a virtual table or say array to store the fetched details. 2)ON页面加载从服务器获取记录,并创建一个虚拟表或说一个数组来存储获取的详细信息。 Now send the response to the requesting page from this array.(Here only for first time when page loads it will call server whereas rest all the page click will be taken from the virtual table which is created as the result of page load) But i am worried how do i maintain the virtual table or array as the system is webbased and multiple user will be interacting at a time. 现在将响应从此数组发送到请求的页面(仅在第一次加载页面时会调用服务器,而其余的所有页面点击都将从页面加载结果创建的虚拟表中获取)我很担心如何维护虚拟表或数组,因为系统是基于Web的,并且多个用户同时进行交互。

Please help me to carry on with the right way. 请帮助我以正确的方式进行。 Incase you have better method then this please let me know. 如果您有更好的方法,那么请告诉我。

Thankyou in advance. 先感谢您。

It is not good practice to load complete record. 加载完整记录不是一个好习惯。 You can use pagination in the following way 您可以通过以下方式使用分页

Front-End 前端

in front end handle the request sent to the controller. 在前端处理发送给控制器的请求。

var data = {'offset': offset};
        var url = "http://localhost/project/controller/listData";
        $http.post(url, $.param(data), config).then(function (data) {
               if (data.data.Code == '200') { // if query returned some data
                for (var i in data.data.result) {
                    $scope.dataSet.push(data.data.result[i]);
                }
                offset += 5;

            }

        }, function (error) {
            console.log("error");

        });

PHP 的PHP

in your controller method use limit and offset when fetching data from database 从数据库中获取数据时,在控制器方法中使用限制和偏移量

public function listData()
{
    $offset = $this->input->post('offset');

    $query = $this->db->get('dataTable', 5, $offset);

    if ($query->num_rows() > 0) {
        $result = $query->result_array();

        $response = array(
            'Code' => '200',
            'Message' => 'Success!!!',
            'result' => $result
        );
        echo json_encode($response);
    } else {
        $response = array(
            'Code' => '203',
            'Message' => 'no data fetched!!',
        );
        echo json_encode($response);
    }

}

PS My suggestion would be to use Infinite scroll for pagination, there are a lot of examples out there try some. PS我的建议是使用无限滚动进行分页,有很多示例可以尝试一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM