PHP pagination for loop (trying to limit the pagination tabs) - php

I am trying to make a pagination for my project,
Im am trying to learn some new skills and decided to start an old project i started a while ago and thought everything was going well and thought i had succeeded...
With the example data I have in my database everything seemed perfect, however when I imported more example data I noticed the pagination tabs were getting very long
currently i have them working as 1|2|3|4|5|6|7|8|9|10
but after i imported more data the list went onto 50+
the PHP code i am using is
<?php
for ($x = 1; $x <= $page_neeed; $x++) {
if ($page==$x){$class="active";}else{$class="";};
echo "<li class='page-item $class'><a class='page-link' href='example.php?page=$x'>$x</a></li>";
}
?>
I have had a look at other sites and decided to have something like 1|2|3|...|50|51|52
For the life of me, I can't get my head around how to approach this.
if you could point me in the right direction that would be fantastic as I don't know what to even search to try to solve this.

After digging around with the links that Tim Lewis gave me in the comments on the OP I soon realised it was more complex than I first thought.
But....
I have managed to get a better pagination than I first had, almost the way I need.
I thought I would post the code here in case someone else could use it.
<?php
$max = $pages_needed-4;
//if we need more than 6 pages
if ($pages_needed > 6) {
//seeing if the current page is between 5 and the last page -4
if (($page >= 5 && $page < $max)) {
$lower_page = $page - 2;
$next_page = $page + 1;
for ($x = $lower_page; $x <= $next_page; $x++) {
if ($page == $x) {
$class = "active";
} else {
$class = "";
}
echo "<li class='page-item $class'><a class='page-link' href='$share_url/category/$category_slug?page=$x'>$x</a></li>";
}
echo "<li class='page-item'><span class='page-link'>...</span></li>";
echo "<li class='page-item $class'><a class='page-link' href='$share_url/category/$category_slug?page=$pages_needed'>$pages_needed</a></li>";
};
//seeing if the current page is near the end
if (($page >= $max && $page < $pages_needed+1)) {
$lower_page = $page - 2;
$next_page = $page + 1;
echo "<li class='page-item'><a class='page-link' href='$share_url/category/$category_slug?page=1'>1</a></li>";
echo "<li class='page-item'><span class='page-link'>...</span></li>";
for ($x = $lower_page; $x <= $pages_needed; $x++) {
if ($page == $x) {
$class = "active";
} else {
$class = "";
}
echo "<li class='page-item $class'><a class='page-link' href='$share_url/category/$category_slug?page=$x'>$x</a></li>";
}
};
//seeing if the current page is near the start
if (($page >= 1 && $page < 5)) {
$lower_page = $page - 2;
$next_page = $page + 1;
for ($x = 1; $x <= 5; $x++) {
if ($page==$x){$class="active";}else{$class="";};
echo "<li class='page-item $class'><a class='page-link' href='$share_url/category/$category_slug?page=$x'>$x</a></li>";
}
echo "<li class='page-item'><span class='page-link'>...</span></li>";
echo "<li class='page-item $class'><a class='page-link' href='$share_url/category/$category_slug?page=$pages_needed'>$pages_needed</a></li>";
};
}
//if we need more less 6 pages
else{
for ($x = 1; $x <= $pages_needed; $x++) {
if ($page==$x){$class="active";}else{$class="";};
echo "<li class='page-item $class'><a class='page-link' href='$share_url/category/$category_slug?page=$x'>$x</a></li>";
}
}
?>
This alows me to have 1|2|3|4|5|6
When on page 5 it goes 3|4|[5]|6|...|30|
On the last 6 pages it gives you 1|...|26|27|28|29|30
I'm pretty sure someone could simplify this but I'm still learning and hope this helps someone else.

Related

php navigation pages issue

I have a strange problem with my navigation next/previous code. Everything works fine but random after I click on the next page, the whole navigation is gone. And when I change the pagina variable from page 4 to 5 for example, does everything work again, but pagina 4 keeps removing the navigation. I don't know why it occurs?
On the top of my page do I have:
if (isset($_GET['pagina']) && $_GET['pagina']!="") {
$page_no = $_GET['pagina'];
}
else {
$page_no = 1;
}
$total_records_per_page = 100;
$offset = ($page_no-1) * $total_records_per_page;
$previous_page = $page_no - 1;
$next_page = $page_no + 1;
$adjacents = "2";
And on the bottom of my page
$taal = htmlspecialchars(addslashes($_GET['taal']));
if($page_no > 1){
echo <a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$previous_page."'>Vorige</a>";
}
if ($total_no_of_pages <= 10){
for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
if ($counter == $page_no) {
echo "<a class='active'>".$counter."</a>";
}
else
{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
}
elseif ($total_no_of_pages > 10){
if($page_no <= 4) {
for ($counter = 1; $counter < 8; $counter++){
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}
else {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
echo "<a>...</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$second_last."'>".$second_last."</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$total_no_of_pages."'>".$total_no_of_pages."</a>";
}
elseif($page_no > 4 && $page_no < $total_no_of_pages - 4) {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=1'>1</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=2'>2</a>";
//echo "<li><a>...</a></li>";
for (
$counter = $page_no - $adjacents;
$counter <= $page_no + $adjacents;
$counter++
) {
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}else{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
echo "<a>...</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$second_last."'>".$second_last."</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$total_no_of_pages."'>".$total_no_of_pages."</a>";
}
else {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=1'>1</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=2'>2</a>";
echo "<a>...</a>";
for (
$counter = $total_no_of_pages - 6;
$counter <= $total_no_of_pages;
$counter++
)
{
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}
else{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
}
}
if($page_no < $total_no_of_pages) {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$next_page."'>Volgende</a>";
}
$taal is language,
$page_no is which page where I'm on
$zoekveld is search field to search on
$pagina is pagenumber in url
I think it has something to do with my GET search field (Zoekveld) but why occurs it after a couple of pages?
Thanks in advance

pagination in php with mysql

I wonder how to easily make pagination in PHP with MySQL.. May I ask you a favor of you?
This my code have not error... take this code with your project implement..
<ul class="pagination justify-content-end">
<?php
$sql = "SELECT * FROM json_data";
$rs_result1 = mysqli_query($connect, $sql);
$row = mysqli_num_rows($rs_result1);
//print_r($rs_result1); die();
$limit = 10;
$total_records = $row;
//$total_pages = ceil($total_records / $limit)-220;
// calculate total pages
$total_pages = ceil($row / $limit);
$prev = $page-1;
$next = $page+1;
$pagination_buttons =5;
$last_page = $total_pages;
$half = floor($pagination_buttons/2);
//echo '<ul class="pagination">';
if($page >= 5){
echo '<li>First</li>';
}
if($page < $pagination_buttons AND ($last_page == $pagination_buttons OR $last_page > $pagination_buttons)){
for($i=1; $i<=$pagination_buttons; $i++){
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
if($last_page > $pagination_buttons){
echo '<li>Next</li>';
}
}
else if($page >= $pagination_buttons AND $last_page > $pagination_buttons){
if(($page+$half) >= $last_page){
echo '<li>Previous</li>';
for ($i=($last_page-$pagination_buttons)+1; $i<=$last_page; $i++) {
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
}
else if(($page+$half) < $last_page){
echo '<li>Previous</li>';
for ($i=($page-$half); $i<=($page+$half); $i++) {
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
echo '<li>Next</li>';
}
}
if($page != $total_pages && $total_pages >= 6){
echo '<li>Last</li>';
} ?>
i solve it my code in pagination in php with mysql.. and hope may help this code your pagination project...
I coded pagination code for my site in php and mysql.. I wonder how to easily make pagination in PHP with MySQL.. May I ask you a favor of you? This my code have not error... take this code with your project implement..

how to set first page pagination link to active by default

This code only shows one of the pagination links as active if I click on it first. But on page load I want number "1" to be active by default.
if(!empty($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 1;
}
for ($page = 1; $page <= $number_of_pages; $page++) {
if ($_GET['page'] == $page) {
echo "<li class='pagination__page--active'><span>{$page}</span></li>";
}
else {
echo "<li><a href='product-category/$page_url/page/{$page}'>{$page}</a></li>";
}
}
Is your problem, that only on page 1 it doesn't show the correct one as active?
If so, your problem is that you are probably not sending a 'page' with your GET request. The easiest way to solve this is:
$curr_page = (isset($_GET['page'])) ? $_GET['page'] : 1;
for ($page = 1; $page <= $number_of_pages; $page++) {
if ($curr_page == $page) {
echo "<li class='pagination__page--active'><span>{$page}</span></li>";
}
else {
echo "<li><a href='product-category/$page_url/page/{$page}'>{$page}</a></li>";
}
}

Limiting pages in php pagination

I want to limit the number of pages displayed on my pagination php script below. This was a script made a few years back for me, and although I have read through similar problems on here, their coding is very different.
Any help with this would be really appreciated.
Here is my current script:
<?php
if ($max_pages>1) {
echo "<br>";
if ($page>0) {
echo 'Previous';
}
for ($x=0;$x<$max_pages;$x++) {
if ($page<>$x) {
echo ''.($x+1).'';
}
else {
echo '<span class="pagination">'.($x+1).'</span>';
}
}
if (($page+1<>$max_pages)) {
echo 'Next';
}
}?>
Your current script cycles $x between 0 and $max_pages.
What you can do is first replace them with $from_page and $to_page:
$from_page = 0;
$to_page = $max_pages;
...
for ($x=$from_page; $x < $to_page; $x++)
at which point the script will work just as before.
Now if you want to only display from $N pages before to $N pages after $page,
$N = 5; // display 5+5+1 = 11 pages
$from_page = $page - $N; if ($from_page < 0) $from_page = 0;
$to_page = $from_page + 2*$N+1; if ($to_page > $max_pages) $to_page = $max_pages;
$from_page = $to_page - 2*$N-1; if ($from_page < 0) $from_page = 0;
Not the most elegant way perhaps, but it will try to fit an 11-page area centered on the current page. You may want to also display a link to pages 0 and $max_pages-1.
MRE version
<?php
if ($max_pages>1)
{
$N = 5; // display 5+5+1 = 11 pages
$to_page = min($max_pages, max(0, $page - $N) + 2*$N+1);
$from_page = max($to_page - 2*$N-1, 0);
echo "<br>";
if ($page > 0)
{
echo 'Previous';
}
for ($x=$from_page; $x < $to_page; $x++) {
if ($page != $x) {
echo ''.($x+1).'';
}
else {
echo '<span class="pagination">'.($x+1).'</span>';
}
}
if (($page+1<>$max_pages)) {
echo 'Next';
}
}?>

PHP Pagination Problem

I'm having a problem with my PHP pagination for a project.
It almost works but it doesn't seem to display the numbers correctly.
I want only 6 more page numbers to display after the selected and one before;
(also if you are on page one display 7 after)
For example:
If on Page 1: 1/2/3/4/5/6/7/8
If on Page 2: 1/2/3/4/5/6/7/8
If on Page 5: 4/5/6/7/8/9/10/11
If on Page 10: 9/10/11/12/13/14/15/16
This is my code so far...
if($page == ceil($NumOfPages) && $page != 1){
for($i = 1; $i <= ceil($NumOfPages)-1; $i++){
if($i > 0){
echo "{$i}";
}
}
}
if ($page == ceil($NumOfPages) ) {
$startPage = $page;
}else{
$startPage = 1;
}
for ($i = $startPage; $i <= $page+6; $i++){
if ($i <= ceil($NumOfPages)){
if($i == $page) {
echo "<a href='/page/$i/' title='View movies page $i' id='pagelisel'>$i</a> ";
}else{
echo "<a href='/page/$i/' title='View movies page $i' id='pageli'>$i</a> ";
}
}
}
Any help would be greatly appreciated,
Thanks!
I assumed that (partly for myself... ;) ):
$page is the selected page
$startPage is the first page number you want to show
$numPages is already ceil-ed
First you need to find $startPage. Depending whether $page is the first one (ie has the value one 1, another assumption) or not. Your check is slightly off, as it check if it is equal to the last page.
if($page == 1) {
$startPage = 1;
} else {
$startPage = $page - 1;
}
Then you need to find out the last page number you want to print ($lastPage). So check if $startPage is near the end and set ~$lastPage` accordingly:
if($startPage + 7 > $numPages) {
$endPage = $numPages;
} else {
$endPage = $startPage + 7;
}
Finally, use you for-loop which seem ok, but loop from $startPage to $endPage.
Here's an alternative approach that should work for you as well:
$pageCurrent = $page;
$pagePrevious = $pageCurrent-1;
$pageClass = '';
$pageStart = 1;
$pageEnd = $pageCurrent+6;
$pageMax = ceil($NumOfPages);
if($pageCurrent==1){
echo "1";
}else{
echo "$pagePrevious";
}
for($i = $pageStart; $i <= $pageEnd; $i++){
if($i <= $pageEnd){
if($i == 1 && $pageCurrent != 1){
$pageClass = 'selected';
}else{
$pageClass = '';
}
echo "$i";
}
}

Categories