issue when changing record limit in php dynamic pagination - php

I am learning PHP and was trying to created dynamic pagination to show record form MySQL.
The issue I'm facing is when I change number of record to be show by using SELECT tag it only works once after that it goes back to default value that i have set -> $limit = isset($_POST['records-count'])?$_POST['records-count']:"10"; which is 10.
PHP
include('../config/DbFunction.php');
$obj = new DbFunction();
$limit = isset($_POST['records-count'])?$_POST['records-count']:"10";
$page = isset($_GET['page'])? $_GET['page']:"1";
$start_data = ($page-1)*$limit;
$rs = $obj->view_course($limit,$start_data);
//print_r($limit); // to check limit value
$course_row = mysqli_fetch_row($obj->view_course1());
$total_records = $course_row[0];
$total_page = ceil($total_records/$limit);
$next = $page + 1 > $total_page? $total_page : $page +1;
$prev = $page - 1 == 0? 1 : $page - 1;
?>
Form
<form method="post" action="#">
Records: <select name="records-count" id="records_count">
<option disabled="disabled" selected="selected">Limit</option>
<?php foreach([20,50,100,200] as $limit): ?>
<option <?php if( isset($_POST["records_count"]) && $_POST["records_count"] == $limit) echo "selected" ?> value="<?= $limit; ?>"><?= $limit; ?></option>
<?php endforeach; ?>
</select>
</form>
Pagination
<div class="pagination">
<ul class="pagination pagination-default">
<li class='page-item'><a class='page-link' href="view-course.php?page=<?= $prev; ?>">Previous</a></li>
<?php
for ($i=1; $i<=$total_page; $i++) {
echo "<li class='page-item'><a class='page-link' href='view-course.php?page=".$i."'>".$i."</a></li>";
}
?>
<li class='page-item'><a class='page-link' href="view-course.php?page=<?= $next; ?>">Next</a></li>
</ul>
</div>

you may need to store and retrieve it from session. Session will allow to store variable in server
you need to start the session before send anything to client.
session_start();
if(isset($_POST['records-count']))
$_SESSION["records-count"] = $_POST['records-count'];
last thing to note is:
session variable will be updated in next requests.

After so many try I got the solution by using GET method
So i Change
$limit = isset($_POST['records-count'])?$_POST['records-count']:"10";
<div class="pagination">
<ul class="pagination pagination-default">
<li class='page-item'><a class='page-link' href="view-course.php?page=<?= $prev; ?>">Previous</a></li>
<?php
for ($i=1; $i<=$total_page; $i++) {
echo "<li class='page-item'><a class='page-link' href='view-course.php?page=".$i."'>".$i."</a></li>";
}
?>
<li class='page-item'><a class='page-link' href="view-course.php?page=<?= $next; ?>">Next</a></li>
</ul>
</div>
to
if (isset($_POST['records-count'])) {
$limit = $_POST['records-count'];
} else {
$limit = empty($_GET['records'])? "2":$_GET['records'];
}
<div class="pagination">
<ul class="pagination pagination-default">
<li class='page-item'><a class='page-link' href="view-course.php?page=<?= $prev; ?>&records=<?= $limit; ?>">Previous</a></li>
<?php
for ($i=1; $i<=$total_page; $i++) {
echo "<li class='page-item'><a class='page-link' href='view-course.php?page=".$i."& records=".$limit."'>".$i."</a></li>";
}
?>
<li class='page-item'><a class='page-link' href="view-course.php?page=<?= $next; ?>&records=<?= $limit; ?>">Next</a></li>
</ul>
</div>
If anyone know more efficient or better way then this plz let me know.

Related

Could not get the id from the url when switching pages in pagination

I'm having trouble getting the id from the URL when i'm switching to another page using the pagination, I need this id from the database to view the contents in the navbar when i switch to another page. When I try to switch to pages the contents in the navbar will not appear. Here is my code
<?php
$limit = 5;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$start = ($page - 1) * $limit;
$sql = "SELECT * FROM invoice LIMIT $start, $limit";
$result1 = $conn->query("SELECT count(id) AS refNo FROM invoice");
$query = mysqli_query($conn,$sql);
$custCount = $result1->fetch_all(MYSQLI_ASSOC);
$total = $custCount[0]['refNo'];
$pages = ceil($total / $limit);
$previous = $page - 1;
$next = $page + 1;
$id = $_GET['id2'];
?>
<h3 class="text-center" style="color:white">Student Schedules</h3>
<label for="course" style="color:white">Invoices And Records<br></label>
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item">
<a class="page-link" href="viewUserSchedule.php?id=<?php $id?>&page=<?= $previous; ?
>">Previous</a></li>
<?php for($i = 1; $i <= $pages; $i++) : ?>
<li class="page-item"><a class="page-link" href="viewUserSchedule.php?id=<?php $id?
>&page=<?= $i; ?>
"><?= $i ?></a></li>
<?php endfor; } ?>
<li class="page-item"><a class="page-link" href="viewUserSchedule.php?id=<?php $id?
>&page=<?= $next; ?>">Next</a></li>
</ul>
</nav>
Just change this:
<?php $id?>
to this:
<?= $id ?>
or this:
<?php echo $id ?>
As I can see in your codes, you have simple mistake in your codes.
You are using:
<?php $id?>
and for other variables like $previous you are using:
<?= $previous; ?>
So <?= is shortcode for <?php echo and in printing your $id variable you missed the echo
So, you should use :
<?php echo $id?>
Or
<?= $id?>
so try this code, and it would work for you:
<h3 class="text-center" style="color:white">Student Schedules</h3>
<label for="course" style="color:white">Invoices And Records<br></label>
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item">
<a class="page-link" href="viewUserSchedule.php?id=<?php echo $id?>&page=<?= $previous; ?>">Previous</a></li>
<?php for($i = 1; $i <= $pages; $i++) : ?>
<li class="page-item"><a class="page-link" href="viewUserSchedule.php?id=<?php echo $id?>&page=<?= $i; ?>"><?= $i ?></a></li>
<?php endfor; } ?>
<li class="page-item"><a class="page-link" href="viewUserSchedule.php?id=<?php echo $id?>&page = <?= $next; ?>">Next</a></li>
</ul>
</nav>

Page view in php

I have to make a small adjustment on a website, the problem comes on the page.
For example I have a page with 11 button, these 1 I do not have to show them at once, but I have for example I have to show the first 5 and then the page has to be moving but I never have to stop showing 5 results on the page.
I leave reference images:
This is the way the page does NOT have to look:
This is the way the page should look:
for example if I get to 7 that 3 is no longer shown and shows me 8
This is my code
<nav>
<ul class="pagination">
<?php if ($pagina == 1) : ?>
<li class="page-item disabled">
<a class="page-link" href="">
<span class="ti-arrow-left"></span>
</a>
</li>
<?php else : ?>
<li class="page-item">
<a class="page-link" href="?pagina=<?php echo $pagina - 1 ?>">
<span class="ti-arrow-left"></span>
</a>
</li>
<?php endif; ?>
<!---------------------------------------------------------------------->
<?php
for ($i = 1; $i <= $numeroDePaginas; $i++) {
if ($pagina == $i) {
echo "<li class='page-item active'>
<a class='page-link' href='?pagina=$i'>$i</a>
</li>";
} else {
if (($i + 2) < $pagina || ($i - 2) > $pagina) {
echo "<li class='page-item'>
<a class='page-link' href='?pagina=$i'>$i</a>
</li>";
}
}
}
?>
<!---------------------------------------------------------------------->
<?php if ($pagina == $numeroDePaginas) : ?>
<li class='page-item disabled'>
<a class='page-link' href=''>
<span class='ti-arrow-right'></span>
</a>
</li>
<?php else : ?>
<li class='page-item'>
<a class='page-link' href="?pagina=<?php echo $pagina + 1 ?>">
<span class='ti-arrow-right'></span>
</a>
</li>
<?php endif; ?>
</ul>
</nav>
Here you go. I have added next_pages variable and previous_pages variables, which will make sure to get the pagina - 2 and pagina + 2 if exists. Let me know if you need help understanding it.
<nav>
<?php
$next_pages = ($pagina + 2) <= $numeroDePaginas ? $pagina + 2 : $numeroDePaginas;
$previous_pages = ($pagina - 2) >= 1 ? $pagina - 2 : 1;
?>
<ul class="pagination">
<?php if ($pagina == 1) : ?>
<li class="page-item disabled">
<a class="page-link" href="">
<span class="ti-arrow-left"></span>
</a>
</li>
<?php else : ?>
<li class="page-item">
<a class="page-link" href="?pagina=<?php echo $pagina - 1 ?>">
<span class="ti-arrow-left"></span>
</a>
</li>
<?php endif; ?>
<!---------------------------------------------------------------------->
<?php
for ($i = $previous_pages; $i <= $next_pages; $i++) {
if ($pagina == $i) {
echo "<li class='page-item active'>
<a class='page-link' href='?pagina=$i'>$i</a>
</li>";
} else {
echo "<li class='page-item'>
<a class='page-link' href='?pagina=$i'>$i</a>
</li>";
}
}
?>
<!---------------------------------------------------------------------->
<?php if ($pagina == $numeroDePaginas) : ?>
<li class='page-item disabled'>
<a class='page-link' href=''>
<span class='ti-arrow-right'></span>
</a>
</li>
<?php else : ?>
<li class='page-item'>
<a class='page-link' href="?pagina=<?php echo $pagina + 1 ?>">
<span class='ti-arrow-right'></span>
</a>
</li>
<?php endif; ?>
</ul>
</nav>
Edit with more explaination:
I have added a variable called next_pages, this variable checks if the current page (ex: 9) + 2 is smaller than or equal to "numeroDePaginas" variable, I have done this to make sure that we never show any pages exceeding the number of pages we have, in case 9+2 is bigger than "numeroDePaginas" we set the variable equal to "numeroDePaginas"
For the previous_pages variable, we make sure that the current page(ex: 3) - 2 is never less than 1, (so it doesn't show 0 or negative pages for example), in case
3-2 is smaller than 1, we set the variable to 1.
Now, I have edited the for loop, to make sure that it starts from the previous_pages variable, and ends with next_pages variable.
Here are a test case:
$pagina = 7;
$numeroDePaginas = 9;
//$next_pages will be = 9 (i.e 7+2)
//$previous_pages will be = 5 (i.e 7-2)
for loop would print pages from 5 to 9.

How to do I properly send variables using pagination?

I currently am having trouble using pagination. The first page is great, and the display on the second and third pages are correct, but the actual $_Get variables are not being carried over onto the second page and third page. Where would I put the actual $_Get Variables to go onto the next pages? Also I am using a form to pass the variables as a hidden input, that again work on the first page, but revert to empty on the second and third pages. I have read about decoupling and using sessions, but have tried implementing session variables at the top to no avail. How would I get the session to go to the second and third page. Thank you!
session_start();
include "dbhReal.inc.php";
include "header.php";
$timeSelected = $_GET['time'];
$dateSelected = $_GET['date'];
$ShownDate = date('M-d-y', strtotime("$dateSelected"));
$Null = '00:00:00';
<!--Form is below, left out the queries for space sake-->
echo '
<div class="col-sm-6">
<div class="card h-100">
<img class="card-img-top" src="../PhotoUploads/uploads/'.$userPic['Link1'].'" height="350" width="400" alt="anotherOne"/>
<div class="card-body">
<h5 class="card-title">'.$row['firstNameP'].' '.$row['lastNameP'].'</h5>
<p class="card-text">'.$row['briefDescription'].'</p>
<form action="settingTheSession.php" method="GET">
<input type="hidden" name="time" value='.$timeSelected.'>
<input type="hidden" name="date" value='.$dateSelected.'>
<input type="hidden" name="idNumber" value='.$boookingNumber.'>
<button type="submit" class="btn btn-primary" name="buttonBookprofileSearch">See Profile</button>
</form>
</div>
</div>
</div>';
<!--Pagnation-->
<ul class="pagination justify-content-center">
<li class="page-item" <?php if($page_no <= 1){ echo "class='page-item disabled'"; } ?>>
<a class='page-link' <?php if($page_no > 1){ echo "href='?page_no=$previous_page'"; } ?>>Previous</a>
</li>
<?php
if ($total_no_of_pages <= 10){
for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
if ($counter == $page_no) {
echo "<li class='page-item active'><a class='page-link'>$counter</a></li>";
}else{
echo "<li class='page-item'><a class='page-link' href='?page_no=$counter'>$counter</a></li>";
}
}
}
elseif($total_no_of_pages > 10){
if($page_no <= 4) {
for ($counter = 1; $counter < 8; $counter++){
if ($counter == $page_no) {
echo "<li class='page-item active'><a class='page-link'>$counter</a></li>";
}else{
echo "<li class='page-item' ><a class='page-link' href='?page_no=$counter'>$counter</a></li>";
}
}
echo "<li class='page-item'><a>...</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=$second_last'>$second_last</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=$total_no_of_pages'>$total_no_of_pages</a></li>";
}
elseif($page_no > 4 && $page_no < $total_no_of_pages - 4) {
echo "<li class='page-item'><a class='page-link' href='?page_no=1'>1</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=2'>2</a></li>";
echo "<li class='page-item'><a class='page-link'>...</a></li>";
for ($counter = $page_no - $adjacents; $counter <= $page_no + $adjacents; $counter++) {
if ($counter == $page_no) {
echo "<li class='page-item active'><a class='page-link'counter</a></li>";
}else{
echo "<li class='page-item'><a class='page-link' href='?page_no=$counter'>$counter</a></li>";
}
}
echo "<li class='page-item'><a class='page-link'>...</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=$second_last'>$second_last</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=$total_no_of_pages'>$total_no_of_pages</a></li>";
}
else {
echo "<li class='page-item'><a class='page-link' href='?page_no=1'>1</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=2'>2</a></li>";
echo "<li class='page-item'><a class='page-link'>...</a></li>";
for ($counter = $total_no_of_pages - 6; $counter <= $total_no_of_pages; $counter++) {
if ($counter == $page_no) {
echo "<li class='page-item active'><a class='page-link'>$counter</a></li>";
}else{
echo "<li class='page-item'><a class='page-link' href='?page_no=$counter'>$counter</a></li>";
}
}
}
}
?>
<li class="page-item" <?php if($page_no >= $total_no_of_pages){ echo "class='disabled'"; } ?>>
<a class="page-link" <?php if($page_no < $total_no_of_pages) { echo "href='?page_no=$next_page'"; } ?>>Next</a>
</li>
<?php if($page_no < $total_no_of_pages){
echo "<li class='page-item'><a class='page-link' href='?page_no=$total_no_of_pages'>Last ››</a></li>";
} ?>
</ul>
<div class="text-center">
<strong>Page <?php echo $page_no." of ".$total_no_of_pages; ?></strong></div>
<br />
At first try not to reinvent the wheel. There are a lot of packages out there which deal with pagination: https://packagist.org/packages/voku/pagination?query=pagination
Secondly, considering your question, as PHP is server side you need to pass the needed data to every page/url you navigate.
So like the following example from your code:
echo "<li class='page-item'><a class='page-link' href='?page_no=1'>1</a></li>";
You will need the add the wanted arguments to the href query string. Like you are already doing with the 'page_no' parameter.
echo "<li class='page-item'><a class='page-link' href='?page_no=1&date=". $dateSelected ."'>1</a></li>";
In that manner you will have access to the wanted arguments on the next pages.

pagination using post id

Am new to php, i am try to use pagenation , where i post a unique id to a page then use that to load content with the respective id then pagenate all the content with that respective id.
Here below is what have been trying
<?php
if (isset($_GET["post"]))
{
$page = $_GET["post"];
} else
{
$page = 1;
};
$limit = 2;
$total_records = $pagination;
$total_pages = ceil($total_records/$limit);
$lastpage = ceil($total_pages/$limit);
$page != 0;
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
//lastpage is = total pages / items per page, rounded up.
if($prev == 0)
$prev = 1;
$next <= $lastpage;
//if no page var is given, default to 1.
$pagLink = "<ul class='pagination pagination-circle' class='justify- content-center'>";
$pagLink .= "<li class='page-item active'>
<a class='page-link black' href='home.php?post=".$prev."' aria-label='Back'>
<span aria-hidden='true'>«</span>
<span class='sr-only'>Next</span>
</a>
</li>";
for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<li class='page-item active'><a class='page-link' href='home.php?post=".$i."'>".$i."</a></li> ";
};
$pagLink .= "<li class='page-item pg-red active'>
<a class='page-link black' href='home.php?post=".$next."' aria-label='Next'>
<span aria-hidden='true'>»</span>
<span class='sr-only'>Next</span>
</a>
</li>";
echo $pagLink . "</ul>";
?>
Here is how i want to pagenate contents per id
enter image description here

pagination next and previous button

hello guys can you help me out ?
i have a problem in pagination
i just want to disable the next and previous
button in my pagination if it is
no item left
$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "final";
$con= mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysqli_select_db($con, "final") or die ("no database");
$pagination_sql = "SELECT * FROM `ongoing` WHERE approved='approve'";
$run_pagination = mysqli_query($con, $pagination_sql);
$count = mysqli_num_rows($run_pagination);
$total_pages = ceil($count/$per_page);
echo "<ul class='pagination'>";
echo "<li class='page-item'><a class='page-link' href='ongoing.php?page=".($page-1)."' class='button'>
<span aria-hidden='true'>«</span>
<span class='sr-only'>Previous</span>
</a></li>";
for($i=1;$i<=$total_pages;$i++){
echo'<li><a class="page-link" href="ongoing.php?page='.$i.'">'.$i.'</a></li>';
};
echo "<li class='page-item'><a class='page-link' href='ongoing.php?page=".($page+1)."' class='button'>
<span aria-hidden='true'>»</span>
<span class='sr-only'>Next</span>
</a></li>";
echo "</ul>";
?>
and here's the condition of per page
$per_page = 10;
if(isset($_GET['page'])){
$page = $_GET['page'];
}else{
$page= 1;
}
$start_from = ($page-1) * $per_page;
I think you just need to adjust your code as below:
echo "<ul class='pagination'>";
if($page == 1) {
$disable_prev = 'disabled';
$prev_url = "javascript:void(0);";
} else {
$disable_prev = '';
$prev_url = "ongoing.php?page=".($page-1);
}
echo "<li class='page-item ".$disable_prev."'><a class='page-link' href='".$prev_url."' class='button'>
<span aria-hidden='true'>«</span>
<span class='sr-only'>Previous</span>
</a></li>";
for($i=1;$i<=$total_pages;$i++){
echo'<li><a class="page-link" href="ongoing.php?page='.$i.'">'.$i.'</a></li>';
};
if($page+1 == $total_pages) {
$disable_next = '';
$next_url = "ongoing.php?page=".($page+1);
} else {
$disable_next = 'disabled';
$next_url = "javascript:void(0);";
}
echo "<li class='page-item ".$disable_next."'><a class='page-link' href='".$next_url."' class='button'>
<span aria-hidden='true'>»</span>
<span class='sr-only'>Next</span>
</a></li>";
echo "</ul>";
Then you can also add some CSS to make disabled li little visible or faded than other
Try:
if($i != NULL){ //NULL or Empty
//Code here
}

Categories