pagination in php

Solutions on MaxInterview for pagination in php by the best coders in the world

showing results for - "pagination in php"
Sophie
18 May 2016
1
2<html>
3<head>
4    <title>Pagination</title>
5    <!-- Bootstrap CDN -->
6    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
7    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
8    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
9</head>
10<body>
11    <?php
12
13        if (isset($_GET['pageno'])) {
14            $pageno = $_GET['pageno'];
15        } else {
16            $pageno = 1;
17        }
18        $no_of_records_per_page = 10;
19        $offset = ($pageno-1) * $no_of_records_per_page;
20
21        $conn=mysqli_connect("localhost","my_user","my_password","my_db");
22        // Check connection
23        if (mysqli_connect_errno()){
24            echo "Failed to connect to MySQL: " . mysqli_connect_error();
25            die();
26        }
27
28        $total_pages_sql = "SELECT COUNT(*) FROM table";
29        $result = mysqli_query($conn,$total_pages_sql);
30        $total_rows = mysqli_fetch_array($result)[0];
31        $total_pages = ceil($total_rows / $no_of_records_per_page);
32
33        $sql = "SELECT * FROM table LIMIT $offset, $no_of_records_per_page";
34        $res_data = mysqli_query($conn,$sql);
35        while($row = mysqli_fetch_array($res_data)){
36            //here goes the data
37        }
38        mysqli_close($conn);
39    ?>
40    <ul class="pagination">
41        <li><a href="?pageno=1">First</a></li>
42        <li class="<?php if($pageno <= 1){ echo 'disabled'; } ?>">
43            <a href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>">Prev</a>
44        </li>
45        <li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
46            <a href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + 1); } ?>">Next</a>
47        </li>
48        <li><a href="?pageno=<?php echo $total_pages; ?>">Last</a></li>
49    </ul>
50</body>
51</html>
52
Karl
17 Apr 2016
1try {
2
3    // Find out how many items are in the table
4    $total = $dbh->query('
5        SELECT
6            COUNT(*)
7        FROM
8            table
9    ')->fetchColumn();
10
11    // How many items to list per page
12    $limit = 20;
13
14    // How many pages will there be
15    $pages = ceil($total / $limit);
16
17    // What page are we currently on?
18    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
19        'options' => array(
20            'default'   => 1,
21            'min_range' => 1,
22        ),
23    )));
24
25    // Calculate the offset for the query
26    $offset = ($page - 1)  * $limit;
27
28    // Some information to display to the user
29    $start = $offset + 1;
30    $end = min(($offset + $limit), $total);
31
32    // The "back" link
33    $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">«</a> <a href="?page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
34
35    // The "forward" link
36    $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">›</a> <a href="?page=' . $pages . '" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</span>';
37
38    // Display the paging information
39    echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';
40
41    // Prepare the paged query
42    $stmt = $dbh->prepare('
43        SELECT
44            *
45        FROM
46            table
47        ORDER BY
48            name
49        LIMIT
50            :limit
51        OFFSET
52            :offset
53    ');
54
55    // Bind the query params
56    $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
57    $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
58    $stmt->execute();
59
60    // Do we have any results?
61    if ($stmt->rowCount() > 0) {
62        // Define how we want to fetch the results
63        $stmt->setFetchMode(PDO::FETCH_ASSOC);
64        $iterator = new IteratorIterator($stmt);
65
66        // Display the results
67        foreach ($iterator as $row) {
68            echo '<p>', $row['name'], '</p>';
69        }
70
71    } else {
72        echo '<p>No results could be displayed.</p>';
73    }
74
75} catch (Exception $e) {
76    echo '<p>', $e->getMessage(), '</p>';
77}
queries leading to this page
php inviare pagina htmlhow to design pagination in phppaginate database records in phppagination in php with exampleresponsive pagination in phpphp pagination with mysqlpagination in php htmlhow to display same data in php paginationtotal pagination phptable pagination in phppagination in html and phpphp make simple paginatinphp pagination basiccreate pagination function phppagination in core phppagination function in phpphp pagination mysqlhow to use pagination in phpphp pagination php mysqlpaginate query of sql in phppagination for phpphp pagination handlepage add pagination phpcreate custom pagination in phparray pagination in phpphp pagniationphp in pager create in 10how to make a pagination with phphow to make a php paginationadd custom pagination in page phpquery php paginationsimple php paginationconcept of pagination in phpcore php paginationmake pagination in phphow to code pagination in phphow to do pagination in php using links add pagination in phppagination afaik phpphp function for paginationlang pagination php paginan in phppaginator php creer une pagination phppagination html phppagination php examplehow to build pagination in phpphp pagination example using mysql databasehtml pagination form get request from mysql phpphp 7 table pagination exampledynamic pagination in phppagination php htmlphp table paginationpagintatio in phphow to add pagination on the php pagespagination in custom phpphp paginarhow to do pagination 3cwithout 3e using phppaginatnion in phppagination sql phpfetch data in pagination mysql php 7php sql paginationpagination with srno in code phpphp pagina demophp pagination conceptpagination with php and mysqlget data with pagination in phpphp easy paginationcreate pagination phpcreate a pagination in phphow to add pagination in php mysqli apicustom pagination phpphp pagination for websitepagination using phpphp simple posts paginationcore php pagination codesearch and pagination in phpphp simple pagination scriptphp pagination functionpaginator class phphow to make pagination phpfonction paginate phpbasic pagination php 24result 3eresult 28 29 with pagination phptable php paginationpagination with phppaginate through php forhow add in pagination in phpcreate paginator php limitpaginasi phpbutton pagination phppagination php codecustom pagination using phppagina phptable pagination in html and phphow to create pagination for response in phpphp pagination script pagination in php table dataphp code for paginationpagination source code in phppaging php pagination system phphow to do pagination without phppagination php classhow to work pagination in phpphp simple paginationpagination in sql phpcreate pagination php functionphp pagination codepagination example phpadd pagination to php pagehow to make blog page in php with paginationpagination in phpphp create pagination tabletutorial pagination database phpla pagination en phppagination in php with indexingpagination page 1 2f25 phppagination database phpcreate pagination in phphow to add pagination in core phptable with pagination in html and phpphp paginationpaginator simples phphow to add pagination to a page in php pagination in php codephp build paginationpagination in php using apihtml paginationpagination php sqlpagina web en phppagination logic phpphp pagination excluding last two recordsphp in paginationbest easiest way to do pagination php or html scrolling pagination in phppagination with number in single phppagination tutorial php pagination phtmlpaginate files phpcustom pagination in phppaginazione phpsimple pagination in pdo phppagination in phpsimple way to add pagination in phpsimple pagination in phppage pagination phppagination php with numbershow to do paging page in phpphp wp paginationphp simple pagination examplepagination in php mysqlphp pagination querypaginate in phppaginate phphow to do simple pagination in phppagination code phppagination mechanism phphow to style pagination phpget table data in php with pagination using databaselist pagination in phppagination using php mysqlcreate pagination page button in phppagination code in phppagination in mysql phppagination calculation in phphow to add pagination in phpphp list with paginationhow to make pagination in phphow to paginate results phpphp pagination examplephp pagination for mysql resultspagination mysql in phpclass pagination phppagination condition in phpphp function paginationbest php paginationhow to implement pagination in phphow to do pagination in phppagination package in phpphp simple mysql pagnationphp custom pagination scriptformula pagination phpphp paginatorphp paginagtionpaignate in phpsql pagination phphow pagination work in phpmysql pagination phpphp html paginationimplement pagination in php for static pagesphp pagination example with codepagination in php w3schoolshow to pagination in phpinvoer pagina phpphp paginatephp correct way for paginationphp custom paginationphp pagination librarypagination sql php how tohow to create pagination phphow to write pagination in phpwhat does pagination in php meansphp pagination tablepagination library phpphp pagination code demo with codehow to call pagination phppagination for table phpphp make a next page sqlphp page wiht paginationfunction pagination phpphp pagination linksmake pagination phppagination in php with exampleexamplehow to create pagination in phppagination in php api paginatiom in phptable with pagination phppagination phppaginatione phppaginacion phphow to add pagination for a forloop in html phptable pagination phppagination phppagination in php webslessonhow to set pagination in website using phpphp pagination sent the get listpagination php from databasephp simple pagination examplephp mysql paginationphp custom pagination functionhow to do pagination 3cwithout 3e phppagination function phppagination example in phpsimple pagination page demo in phpdatabase results pagination in phpeasy pagination phppagination from mysql data phpimplement pagination in phpphp paginatinpagination en phppagination on php pagepagination formula phppaging through php exampel codephp pagination table examplehow to create pagination links in phpphp paganationwhat is pagination in phphow to make pagination with phpphp table with paginationpagination in php for displaying tablepaginate in phhtml php sample search result paginationhtml pagination table form get request from mysql phphow to paginate paginate results phppaginate results phpphp array paginationphph paginationpagination in php examplehow to make pagging using phppaging in phppagination php mysqlhow to create pagination with phppagination in php with mysqlpagination in php