简体   繁体   中英

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. (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. 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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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