r/learnphp • u/terminaldisorder • Apr 07 '24
needing help with pagination
hi i am new to learning php and i am working on a personal project. i am trying to figure out pagination. i have managed to get to where i am with my code with the help of youtube and chatgpt and now ive spent several days trying to figure pagination but i keep getting stuck. when i try to implement the pagination, it shows the number values on the bottom but when i click on it nothing happens. it updates the address bar but the page shows blank and only after i hit search it populates the data. could someone point me in the right direction?
this is my code
<?php include 'connect.php'; $recordsPerPage = 10; // Number of records per page $page = isset($_GET\['page'\]) ? $_GET\['page'\] : 1; // Current page number, default is 1 // Count total number of records $totalQuery = "SELECT COUNT(\*) AS total FROM projectnew"; $totalResult = mysqli_query($con, $totalQuery); $totalRow = mysqli_fetch_assoc($totalResult); $totalRecords = $totalRow\['total'\]; $totalPages = ceil($totalRecords / $recordsPerPage); ?>
<!DOCTYPE html>
<html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Search Data</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script> </head> <body> <div class="container my-5">
<form method="post"> <div class="input-group mb-3"> <input type="text" class="form-control" placeholder="Search data" name="search"> <select class="form-select" name="country"> <option value="">All Countries</option> <?php $query = "SELECT DISTINCT country FROM projectnew"; $result = mysqli_query($con, $query); if ($result) { while ($row = mysqli_fetch_assoc($result)) { echo '<option value="' . $row\['country'\] . '">' . $row\['country'\] . '</option>'; } } ?> </select> <!-- Other select options for state, district, and category --> <button class="btn btn-dark btn-sm" name="submit">Search</button> </div> </form> <div class="container my-5"> <table class="table"> <?php if(isset($_POST\['submit'\])){ // Your search and SQL query code $search = $_POST\['search'\]; $country = $_POST\['country'\];
$offset = ($page - 1) * $recordsPerPage;
$sql = "SELECT * FROM projectnew WHERE (id LIKE '%$search%' OR title LIKE '%$search%')";
if(!empty($country)) {
$sql .= " AND country = '$country'";
}
$sql .= " LIMIT $offset, $recordsPerPage";
$result = mysqli_query($con, $sql);
if($result) {
// Display table headers
echo '<thead>
<tr> <th>No.</th> <th>Title</th> <th>Country</th> </tr> </thead>'; // Display search results while($row=mysqli_fetch_assoc($result)){ echo '<tbody> <tr> <td>'.$row\['id'\].'</td> <td><a href="searchdata.php?data='.$row\['id'\].'">'.$row\['title'\].'</a></td> <td>'.$row\['country'\].'</td> </tr> </tbody>'; } } else { echo '<h2 class=text-danger> Data not found</h2>'; } } ?> </table> <!-- Pagination links --> <div class="pagination"> <?php // Display pagination links for ($i = 1; $i <= $totalPages; $i++) { echo '<a href="?page=' . $i . '">' . $i . '</a>'; } ?> </div> </div> </div> </body> </html> i really appreciate any help. thank you.
1
u/colshrapnel Apr 07 '24
Of course it would show you nothing because you're using POST method for your search and "number values on the bottom" are just regular links that use GET method.
You should change your search form's method to GET and add search values to pagination links, like
But first of all you need to learn how to use prepared statements! See https://phpdelusions.net/mysqli#prepare
1
u/ryantxr Apr 07 '24
As programmers we learn by solving hard problems. This is an opportunity to sharpen your debugging skills. Start using a debug log. You can use this small code snippet to set one up. https://gist.github.com/ryantxr/fb2b2fa9fa63b34a1bd9 Log everything then go through the log and see what is happening.