<!-- Step-1 : find how much data show on one page, say=3 --> <!-- Step-2 : fetch the whole data and calculate the number of page --> <!-- Step-3 : run the loop and insert the data in it pagination html code --> <!-- Step-4 : now fetch the data by sql query by using LIMIT AND OFFSET --> <!-- Step-5 : if user button on top menubar pressed then it must provide the page number=1 other wise show error --> <!-- Step-6 : make the current page selected by using if conditon --> <!-- Step-7 : show previous button except on page number = 1 --> <!-- Step-8 : show next except on last --> <?php include "header.php"; ?> <div id="admin-content"> <div class="container"> <div class="row"> <div class="col-md-10"> <h1 class="admin-heading">All Users</h1> </div> <div class="col-md-2"> <a class="add-new" href="add-user.php">add user</a> </div> <div class="col-md-12"> <?php include "config.php"; $limit = 3; /*-- Step-5 : if user button on top menubar pressed then it must provide the page number=1 other wise show error */ if(isset($_GET['page'])){ // if page has some value $page = $_GET['page']; } else{ $page = 1; // if not then store value 1 } /*-- Step-4 : now fetch the data by sql query by using LIMIT AND OFFSET */ $offset = ($page-1)*$limit; $sql = "SELECT * FROM user ORDER BY user_id DESC LIMIT {$offset},{$limit}"; // new query /* $sql = "SELECT * FROM user ORDER BY user_id DESC"; // old query */ $result = mysqli_query($conn, $sql) or die("Query Failed."); if(mysqli_num_rows($result) > 0){ ?> <table class="content-table"> <thead> <th>S.No.</th> <th>Full Name</th> <th>User Name</th> <th>Role</th> <th>Edit</th> <th>Delete</th> </thead> <tbody> <?php while($row = mysqli_fetch_assoc($result)) { ?> <tr> <td class='id' ><?php echo $row['user_id']; ?></td> <td><?php echo $row['first_name'] . " ". $row['last_name']; ?></td> <td><?php echo $row['username']; ?></td> <td><?php if($row['role'] == 1){ echo "Admin"; }else{ echo "Normal"; } ?></td> <td class='edit'><a href='update-user.php?id=<?php echo $row["user_id"];?>' ><i class='fa fa-edit'></i></a></td> <td class='delete'><a href='delete-user.php?id=<?php echo $row["user_id"];?>' ><i class='fa fa-trash-o'></i></a></td> </tr> <?php } ?> </tbody> </table> <?php } $sql1 = "SELECT * FROM user"; $result1 = mysqli_query($conn, $sql1) or die("Query Failed."); if(mysqli_num_rows($result1) > 0){ $total_records = mysqli_num_rows($result1); /*-- Step-1 : find how much data show on one page, say=3 */ /* $limit = 3; // put it on the top */ /*-- Step-2 : fetch the whole data and calculate the number of page */ $total_page = ceil($total_records / $limit); echo "<ul class='pagination admin-pagination'>"; /*-- Step-7 : show previous button except on page number = 1 */ if($page > 1){ echo '<li><a href="users.php?page='.($page - 1).'">Prev</a></li>'; } for ($i = 1; $i <= $total_page; $i++) { /*-- Step-6 : make the current page selected by using if conditon */ if($i==$page){ $active = "active"; } else{ $active = ""; } /*-- Step-3 : run the loop and insert the data in it pagination html code */ echo'<li class="'.$active.'"><a href="users.php?page='.$i.' ">'.$i.'</a></li>'; } /*-- Step-8 : show next except on last */ if($total_page > $page){ echo '<li><a href="users.php?page='.($page + 1).'">Next</a></li>'; } echo "</ul>"; } ?> <!-- /* <li class="active"><a>1</a></li> */ --> </div> </div> </div> </div> <?php include "header.php"; ?>