Forum Moderators: coopster

Message Too Old, No Replies

How do you make extra pages with links?

         

jbearnolimits

12:35 am on Oct 15, 2022 (gmt 0)

Top Contributors Of The Month



So I am aware of what I would need in order to query only say 25 results at a time. But how do you create the extra pages with the remaining results and have the number links to navigate between the pages?

jbearnolimits

12:56 am on Oct 15, 2022 (gmt 0)

Top Contributors Of The Month



Nevermind, it's called Pagination. Just remembered.

jbearnolimits

5:08 pm on Oct 15, 2022 (gmt 0)

Top Contributors Of The Month



Ok, so I may as well ask here. I'm able to get pagination to work when just doing a select query. But if I do a query that requires a where statement I can get the first page but the next pages show 0 results. So I am wondering how to get pagination to pass the variable of the where statement to the next pages?

I am not at the computer now but an example of the query is:

SQL="SELECT * FROM Contacts WHERE type = $type ORDER BY Desc"


It gets to first page of results but not the next... even though it counts the number of pages and provides the links. If I just do "SELECT * FROM Contacts ORDER BY Desc" it has no problem. So obviously it is not passing the type variable or something.

Dimitri

9:36 pm on Oct 15, 2022 (gmt 0)

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



I think you might be looking for the LIMIT clause of the SELECT statement :

[mariadb.com...]

jbearnolimits

11:07 pm on Oct 15, 2022 (gmt 0)

Top Contributors Of The Month



Oh, sorry I forgot I have that in the code too. I haven't been near the computer today so I will put the code on here later tonight.

jbearnolimits

3:40 am on Oct 16, 2022 (gmt 0)

Top Contributors Of The Month



Ok, so here is the code I'm working on. From the main page I select the type I want to filter results by and it sends it via POST. Then on the page the results show up on I have the following (starting after the database connect).

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$Type = $_POST['Type'];
}

$sql = "SELECT * FROM Contacts WHERE Type = '$Type' ORDER BY FirstName ASC";
$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'];
$Type = $row['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";
?>

<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";
$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?page=".($page_number-1)."'> Prev </a>";
}
for ($i=1; $i<=$total_pages; $i++) {
if ($i == $page_number) {
$pageURL .= "<a class = 'active' href='../Filter.php?page="
.$i."'>".$i." </a>";
}
else {
$pageURL .= "<a href='../Filter.php?page=".$i."'>
".$i." </a>";
}
};
echo $pageURL;
if($page_number<$total_pages){
echo "<a href='../Filter.php?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?page='+page;
}
</script>
<?php
} else {
echo "0 results";
}
?>


The result is a page that shows all results (no limit) with number links to other pages at the bottom. Each number link should be another 5 results. But when I click a link to go to another page it shows 0 results and "Notice: Undefined variable: Type in path/Filter.php on line 78". I know I'm missing something basic here I just can't put my finger on it.

jbearnolimits

5:01 am on Oct 16, 2022 (gmt 0)

Top Contributors Of The Month



I got the limit working now. But it still tells me when I click a link to go to another page "0 results" and "Notice: Undefined variable: Type in path/Filter.php on line 78".