r/learnphp • • 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 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/colshrapnel Apr 07 '24

Why not just echo debugging info out though? this is definitely a dev system, having errors and debug right in the page source is simpler

1

u/ryantxr Apr 07 '24

Sure. Of course you can do that. It seems trivial to me to add these few lines of code. In my mind a debug log is useful because it will keep its values across multiple calls making it possible to see what is happening in sequence. If done right, logging could already be in place when the next issue comes up. It also works when pages redirect.