pagination html php

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

showing results for - "pagination html php"
Ronan
13 Jul 2017
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