Forum Moderators: coopster

Message Too Old, No Replies

New Pagination issue

         

jbearnolimits

10:13 pm on Oct 16, 2022 (gmt 0)

Top Contributors Of The Month



I had posted about pagination before but have since solved the issues in that thread. There is a new issue I would like to have a second pair of eyes to take a look at. I'm trying to figure out why pagination seems to be working on the first page of results showing the first 5 rows. But, the second page (which I have now fixed as far as passing the variable to it) returns 0 results even though there are a lot more. It doesn't tell me there are any errors. It just doesn't show any results. Below is the code.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$Type=$_POST['Type'];
}
if (isset($_GET["Type"])) {
$Type = $_GET["Type"];
}
$limit = 5;
// update the active page number
if (isset($_GET["page"])) {
$page_number = $_GET["page"];
}
else {
$page_number=1;
}
// get the initial page number
$initial_page = ($page_number-1) * $limit;
$sql = "SELECT * FROM Contacts WHERE Type = '$Type' ORDER BY FirstName ASC LIMIT $initial_page, $limit";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
// collect value of input field
$id = $row['id'];
$FirstName = $row['FirstName'];
$LastName = $row['LastName'];
$Email = $row['Email'];
$Phone = $row['Phone'];
$HomeAddress = $row['HomeAddress'];
$CreationDate = $row['CreationDate'];
?>

<div class="OuterDatabaseRowType1">
<div class="InnerDatabaseRowType1ProfilID"><ul class="second"><li class="second"><a href="../Profile.php/?id=<?php echo "$id";?>">View Profile</a></li></ul></div>
<div class="InnerDatabaseRowType1Name"><?php echo "$FirstName $LastName";?></div>
<div class="InnerDatabaseRowType1Email"><?php echo "$Email";?></div>
<div class="InnerDatabaseRowType1Phone"><?php echo "$Phone";?></div>
<div class="InnerDatabaseRowType1HomeAddress"><?php echo "$HomeAddress";?></div>
<div class="InnerDatabaseRowType1CreationDate"><?php echo "$CreationDate";?></div>
</div>
<?php
}
$sql = "SELECT COUNT(*) FROM Contacts WHERE Type = '$Type'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_row($result);
$total_rows = $row[0];
echo "</br>";
// get the required number of pages
$total_pages = ceil($total_rows / $limit);
$pageURL = "";
if($page_number>=2){
echo "<a href='../Filter.php?Type=$Type?page=".($page_number-1)."'> Prev </a>";
}
for ($i=1; $i<=$total_pages; $i++) {
if ($i == $page_number) {
$pageURL .= "<a class = 'active' href='../Filter.php?Type=$Type?page="
.$i."'>".$i." </a>";
}
else {
$pageURL .= "<a href='../Filter.php?Type=$Type?page=".$i."'>
".$i." </a>";
}
};
echo $pageURL;
if($page_number<$total_pages){
echo "<a href='../Filter.php?Type=$Type?page=".($page_number+1)."'> Next </a>";
}
?>
<div class="inline">
<input id="page" type="number" min="1" max="<?php echo $total_pages;?>"
placeholder="<?php echo $page_number."/".$total_pages; ?>" required>
<button onClick="go2Page();">Go</button> <br><br>
</div>
<script>
function go2Page()
{
var page = document.getElementById("page").value;
page = ((page><?php echo $total_pages; ?>)?<?php echo $total_pages; ?>:((page<1)?1:page));
window.location.href = '../Filter.php?Type=$Type?page='+page;
}
</script>
<?php
} else {
echo "0 results";
}
?>

jbearnolimits

4:13 am on Oct 17, 2022 (gmt 0)

Top Contributors Of The Month



Got it solved. I was not using the right syntax to pass 2 variables along the URL. I was using /Filter.php?Type=$Type?page=".($page_number-1)." But the right way was /Filter.php?Type=$Type&page=".($page_number-1).".

So, just in case anyone else comes across the same issue this may help.

robzilla

10:00 am on Oct 17, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just a few more things to consider with pagination:

- "First" and "Last" links to quickly navigate to the first and last page of the set (textual or with icons).

- If there are lots of pages, the pagination section can grow too large; you could opt to show only the most useful links. For example, if you have 20 pages and the user is on page 10, I would usually only show "First" "1" "2" "..." "9" "[10]" "11" "..." "19" "20" "Last" or something like that. There are many variations, best to see what works well for your visitors and content type.

- When any page links to page 1 of the paginated set, ideally those links would refer to the URL without the "page" parameter. This is to prevent having people link to, and bots crawl, a URL like /whatever?page=1 when that's the same as /whatever, the URI you're probably referring to in other links.

- Do not link to the current (active) page in the paginated set.

Dimitri

11:19 am on Oct 17, 2022 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



@robzilla's recommendations are very good, as usual.

I would add, do not forget to test your parameters, to be sure $Type is one of the value you are expecting and $page is not out of range. If the parameters are wrong, return 404 to avoid unexpected behavior of your code. Keep in mind that anybody can change these parameters.

jbearnolimits

1:47 pm on Oct 17, 2022 (gmt 0)

Top Contributors Of The Month



Thanks guys. I'll be implementing the tips.