Steve_Berry Posted August 28, 2021 Share Posted August 28, 2021 Hello. I have a page that displays data from a database based on pagination. All works well if I display all records (19 at the moment), but if I choose to view 8, or 10, or less than 19, I see duplicates of the same data, as well as new data not showing. How can I prevent duplications and have new date viewed without having to view all data? include("headsection.php"); include("nav.php"); if(isset($_GET['page'])) { $page = $_GET['page']; } else { $page = 1; } $num_per_page = 20; $start_from = ($page -1) * 2; $sql = "SELECT * FROM people ORDER BY id ASC LIMIT $start_from, $num_per_page"; $result = $con->query($sql); if( $result->num_rows > 0) { ?> <h1 class="center">List of all Users</h1> <table class="container3"> <tr> <td width="auto">PID</td> <td width="auto">Name</td> <td width="auto">Relationship</td> <td width="auto" class="center" colspan="3">Actions</td> </tr> <form action="" method="POST"> <?php while( $row = $result->fetch_assoc()){ echo "<input type='hidden' value='". $row['id']."' name='id'>"; //added echo "<tr>"; echo "<td class='success'>".$row['pid'] . "</td>"; echo "<td class='success'>".$row['name'] . "</td>"; echo "<td class='success'>".$row['relationship'] . "</td>"; echo "<td><a href='view.php?id=".$row['id']."' class='primary' title='view'>V</a></td>"; echo "<td><a href='edit.php?id=".$row['id']."' class='warning' title='edit'>E</a></td>"; echo "<td><a href='delete.php?id=".$row['id']."' class='danger' title='delete'>D</a></td>"; echo "</tr>"; } ?> </form> </table> <?php $pr_query = "SELECT * FROM people ORDER BY id"; $pr_result = $con->query($pr_query); $total_record = mysqli_num_rows($pr_result); //echo $total_record; $total_page = ceil($total_record/$num_per_page); echo "<h3 class='center'>Number of records in database: " . $total_record . "</h3>"; echo "<p class='center'>"; if ($page > 1 ){ echo "<a class='primary' href='users.php?page=".($page-1)."'>Previous</a>"; } for($i=1; $i<$total_page; $i++) { echo "<a class='primary' href='users.php?page=".$i."'>$i</a>"; } if ($i > $page ){ echo "<a class='primary' href='users.php?page=".($page+1)."'>Next</a>"; } echo "</p>"; ?> <br><br> <?php } else { echo "<br><br>No Record Found"; } ?> <?php include("footsection.php"); ?> As the site is localhosted, i am using mysqli - the site will not be online. Thanks in advance for any help. Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted August 28, 2021 Solution Share Posted August 28, 2021 Try $start_from = ($page - 1) * $num_per_page; 1 Quote Link to comment Share on other sites More sharing options...
Strider64 Posted August 28, 2021 Share Posted August 28, 2021 (edited) /* if(isset($_GET['page'])) { $page = $_GET['page']; } else { $page = 1; } */ if ( isset($_GET['page']) ) { $current_page = $_GET['page']; } else { $current_page = 1; } $per_page = 20; // $num_per_page = 20; $offset = $per_page * ($current_page - 1) //$sql = "SELECT * FROM people ORDER BY id ASC LIMIT $start_from, $num_per_page"; $sql = 'SELECT * FROM people ORDER BY id ASC LIMIT ' . $per_page . ' OFFSET ' . $offset . '; I think this will work, but I'm just taking an educated guess. You are going to need know the total number of records, so that you don't go pass the total records in your database table using your previous and next links. HTH Helps? Though I do have pagination working on my own website - https://www.phototechguru.com/ Edited August 28, 2021 by Strider64 1 Quote Link to comment Share on other sites More sharing options...
Steve_Berry Posted August 29, 2021 Author Share Posted August 29, 2021 Thanks to both. It works all works perfectly. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.