How to get query to transfer to subsquent pages when paginating results

前端 未结 2 1481
北恋
北恋 2021-01-25 00:03

I\'ve been going through all the pagination questions and answers on the site, and among all the long-drawn out code and OO solutions, this code is among the shortest and simple

2条回答
  •  迷失自我
    2021-01-25 00:50

    Not sure why you need to do select count(*) every time. I would suggest doing it like this:

    \n", mysqli_connect_error());
        exit();
    }
    
    $perPage = 10;
    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
    $startAt = $perPage * ($page - 1);
    
    /* Send a query to the server */
    if ($result = $mysqli->query(
         "SELECT * FROM table ORDER BY title limit $startAt, $perPage")) {
        $rowCnt = $result->num_rows;
        echo "

    Page: $page

    \n"; /* Fetch the results of the query and show results*/ while( $row = $result->fetch_assoc() ) { printf("%s - %s
    \n", $row['orderDate'], $row['orderDescription']); } /* Show next page, previous page links dynamically */ echo "

    \n"; // generate previous page link if current page no is after 1st page if ($page > 1) printf("Previous Page   \n", $_SERVER["SCRIPT_NAME"], ($page-1)); // generate next page link if we were able to fetch $perPage rows if ($rowCnt == $perPage) printf("Next Page\n", $_SERVER["SCRIPT_NAME"], ($page+1)); echo "

    \n"; /* Destroy the result set and free the memory used for it */ $result->close(); } /* close the connection */ $mysqli->close(); ?>

    I am using new mysqli extension of php, here is the php mysqli reference documentation

提交回复
热议问题