Pagination dupliacates pages on first and last page - php

If I'm on page 2 (?p=2) This is how the pager look like
Första « 1 2 3 » Sista
However if I'm on page 1 or 3 it looks like this
(?p=1) 1 2 3 3 » Sista
(?p=3) Första « 1 1 2 3
What am I doing wrong?
I'm using the logic from another question that was posted on this site but noone seems to experience this problem as I do. Limit pagination page number
function get_paging_info($tot_rows,$pp,$curr_page)
{
$pages = ceil($tot_rows / $pp); // calc pages
$data = array(); // start out array
$data['si'] = ($curr_page * $pp) - $pp; // what row to start at
$data['pages'] = $pages; // add the pages
$data['curr_page'] = $curr_page; // Whats the current page
return $data; //return the paging data
}
function pagin($db,$sql,$limit,$curr_page,$max_pages=7)
{
$res = $db->query($sql);
$count = $res->num_rows;
$paging_info = get_paging_info($count,$limit,$curr_page);
$out[] = "
<nav aria-label='Page navigation'>
<ul class='pagination'>";
if($paging_info['curr_page'] > 1)
{
$out[] = "
<li><a href='?p=1' title='Sida 1'>Första</a></li>
<li>
<a href='?p=". ($paging_info['curr_page'] - 1) ."' aria-label='Föregående'>
<span aria-hidden='true'>«</span>
</a>
</li>";
}
$max = $max_pages;
if($paging_info['curr_page'] < $max)
{
$sp = 1;
}
elseif($paging_info['curr_page'] >= ($paging_info['pages'] - floor($max / 2)) )
{
$sp = $paging_info['pages'] - $max + 1;
}
elseif($paging_info['curr_page'] >= $max)
{
$sp = $paging_info['curr_page'] - floor($max/2);
}
if($paging_info['curr_page'] >= $max)
{
$out[] = "<li><a href='?p=1' title='Sida 1'>1</a></li>";
}
for($i = $sp; $i <= ($sp + $max -1);$i++)
{
if($i > $paging_info['pages']) continue;
if($paging_info['curr_page'] == $i)
{
$out[] = "<li class='active'><span class='strong'>". $i ."</span></li>";
}
else
{
$out[] = "<li><a href='?p=". $i ."' title='Sida ". $i ."'>". $i ."</a></li>";
}
}
if($paging_info['curr_page'] < ($paging_info['pages'] - floor($max / 2)))
{
$out[] = "<li><a href='?p=". $paging_info['pages'] ."' title='Sida ". $paging_info['pages'] ."'>". $paging_info['pages'] ."</a></li>";
}
if($paging_info['curr_page'] < $paging_info['pages'])
{
$out[] = "
<li>
<a href='?p=". ($paging_info['curr_page'] + 1) ."' aria-label='Nästa' title='Page ". ($paging_info['curr_page'] + 1) ."'>
<span aria-hidden='true'>»</span>
</a>
</li>
<li><a href='?p=". $paging_info['pages'] ."' title='Sida ". $paging_info['pages'] ."'>Sista</a></li>";
}
$out[] = "</ul></nav>";
return implode('',$out);
}

I think your problem is due to this two conditions before and after your for loop :
First:
if($paging_info['curr_page'] >= $max)
{
$out[] = "<li><a href='?p=1' title='Sida 1'>1</a></li>";
}
Second:
if($paging_info['curr_page'] < ($paging_info['pages'] - floor($max / 2)))
{
$out[] = "<li><a href='?p=". $paging_info['pages'] ."' title='Sida ". $paging_info['pages'] ."'>". $paging_info['pages'] ."</a></li>";
}
You don't need this conditions because the corresponding pages are already displayed with the for loop.

Try this. I have removed some extra codes from your function. replace your pagin function with following code and try
function pagin($db,$sql,$limit,$curr_page,$max_pages=7)
{
$res = $db->query($sql);
$count = $res->num_rows;
$paging_info = get_paging_info($count,$limit,$curr_page);
$out[] = "
<nav aria-label='Page navigation'>
<ul class='pagination'>";
if($paging_info['curr_page'] > 1)
{
$out[] = "
<li><a href='?p=1' title='Sida 1'>Första</a></li>
<li>
<a href='?p=". ($paging_info['curr_page'] - 1) ."' aria-label='Föregående'>
<span aria-hidden='true'>«</span>
</a>
</li>";
}
$max = $max_pages;
if($paging_info['curr_page'] < $max)
{
$sp = 1;
}
elseif($paging_info['curr_page'] >= ($paging_info['pages'] - floor($max / 2)) )
{
$sp = $paging_info['pages'] - $max + 1;
}
elseif($paging_info['curr_page'] >= $max)
{
$sp = $paging_info['curr_page'] - floor($max/2);
}
for($i = $sp; $i <= ($sp + $max -1);$i++)
{
if($i > $paging_info['pages']) continue;
if($paging_info['curr_page'] == $i)
{
$out[] = "<li class='active'><span class='strong'>". $i ."</span></li>";
}
else
{
$out[] = "<li><a href='?p=". $i ."' title='Sida ". $i ."'>". $i ."</a></li>";
}
}
if($paging_info['curr_page'] < $paging_info['pages'])
{
$out[] = "
<li>
<a href='?p=". ($paging_info['curr_page'] + 1) ."' aria-label='Nästa' title='Page ". ($paging_info['curr_page'] + 1) ."'>
<span aria-hidden='true'>»</span>
</a>
</li>
<li><a href='?p=". $paging_info['pages'] ."' title='Sida ". $paging_info['pages'] ."'>Sista</a></li>";
}
$out[] = "</ul></nav>";
return implode('',$out);
}

Related

Quick PHP Pagination not working

Found a working code on PHP pagination here and i am trying to integrate this code into my search engine. I tried but the pagination function doesn't seem to work please can anybody tell me what's wrong with my code and any possible fixes.
<?php
if(isset($_GET['search']))
{
$search = $_GET['search'];
$condition = '';
$query = explode(" ", $_GET["search"]);
foreach($query as $text)
{
$condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR answer LIKE '%".SQLite3::escapeString($text)."%' OR keywords LIKE '%".SQLite3::escapeString($text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$sql_query = "SELECT * FROM questions WHERE " . $condition;
$result = $db->query($sql_query);
echo $sql_query;
$queryCount = "SELECT COUNT(*) as count FROM questions WHERE " . $condition;
$countRet = $db->querySingle($queryCount);
if ($countRet > 0)
{
$perpage = 1;
$start = isset($_GET['start']) ? $_GET['start']: '';
$max_pages= ceil($countRet / $perpage);
if (!$start) {
$start = 0;
$getQuery = $db->query("SELECT * FROM questions WHERE $condition LIMIT $start, $perpage");
while($row = $getQuery->fetchArray(SQLITE3_ASSOC))
{
echo '<tr><td>'.$row["question"].'</td></tr>';
}
//Pagination Codes
echo "<center>";
$prev = $start - $perpage;
$next = $start + $perpage;
$adjacent = 3;
$last = $max_pages - 1;
if ($max_pages > 1) {
//prev button
if (!$start <= 0)
{
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";
}
if ($max_pages < 7 + ($adjacent * 2)) {
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacent * 2); $counter++) {
if ($i == $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
elseif ($max_pages - ($adjacent * 2) > ($start / $perpage) && ($start / $perpage) > ($adjacent * 2)) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";
$i = $start;
for ($counter = ($start / $perpage)+1; $counter < ($start /$perpage) + $adjacent + 2; $counter++) {
if ($i == $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";
$i = $start;
for ($counter = ($start / $perpage) + 1; $counter <= $max_pages; $counter++) {
if ($i = $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
}
//next button
if (!($start >= $countRet - $perpage)) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
}
echo "</center>";
}
}
else
{
echo '<label>No data found</label>';
}
}
?>
The code above doesn't seem to work for some reason. I have tried to debug but cant't seem to find the error or errors. Any reasonable help would do with this. Thanks in advance.
First you set $start in your ternary logic:
$start = isset($_GET['start']) ? $_GET['start']: '';
After this, you check whether $start exists or not, and if it doesn't, you set it 0:
if (!$start) {
$start = 0;
$getQuery ...
while($row = $getQuery ...
The problem is all of your query and pagination logic is inside of this if conditional; if $GET['start'] is set, then your query and pagination logic will never trigger. And on top of this, all of your outputted <a> links do indeed send the start parameter:
<a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>
To resolve this, simply set your ternary to 0 for the failure condition, and omit the if conditional entirely:
$start = isset($_GET['start']) ? $_GET['start']: 0;
This can be seen in the following:
$start = isset($_GET['start']) ? $_GET['start']: 0;
$max_pages= ceil($countRet / $perpage);
$getQuery = $db->query("SELECT * FROM questions WHERE $condition LIMIT $start, $perpage");
while($row = $getQuery->fetchArray(SQLITE3_ASSOC))
{
echo '<tr><td>'.$row["question"].'</td></tr>';
}
//Pagination Codes
echo "<center>";
$prev = $start - $perpage;
$next = $start + $perpage;
$adjacent = 3;
$last = $max_pages - 1;
if ($max_pages > 1) {
//prev button
if (!$start <= 0)
{
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";
}
if ($max_pages < 7 + ($adjacent * 2)) {
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacent * 2); $counter++) {
if ($i == $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
elseif ($max_pages - ($adjacent * 2) > ($start / $perpage) && ($start / $perpage) > ($adjacent * 2)) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";
$i = $start;
for ($counter = ($start / $perpage)+1; $counter < ($start /$perpage) + $adjacent + 2; $counter++) {
if ($i == $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$perpage'>2</a> .... ";
$i = $start;
for ($counter = ($start / $perpage) + 1; $counter <= $max_pages; $counter++) {
if ($i = $start) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $perpage;
}
}
}
//next button
if (!($start >= $countRet - $perpage)) {
echo " <a href='pricearray.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
}
echo "</center>";

PHP MySQL pagination mysqli_result() error

I'm working on pagination, but it didn't work I tried using mysqli_fetch_array and also tried mysqli_result.
Please guide me if you have some ideas.
For the pagination page I tried a method that I found but still have problem with it.
Here is the code I tried,
<?php
$conn = mysqli_connect("localhost", "root", "", "vm");
include 'paginate.php';
$per_page = 2;
$result = mysqli_query($conn, "SELECT * FROM table1");
$total_results = mysqli_num_rows($result);
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page'])) {
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
$start = 0;
$end = $per_page;
}
} else {
$start = 0;
$end = $per_page;
}
$page = intval($_GET['page']);
$tpages = $total_pages;
if ($page <= 0) {
$page = 1;
}
function paginate($reload, $page, $tpages)
{
$adjacents = 2;
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "";
// previous
if ($page == 1) {
$out .= "<span>" . $prevlabel . "</span>\n";
} elseif ($page == 2) {
$out .= "<li>" . $prevlabel . "\n</li>";
} else {
$out .= "<li>" . $prevlabel . " \n</li>";
}
$pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
$pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out .= "<li class=\"active\"><a href=''>" . $i . "</a></li>\n";
} elseif ($i == 1) {
$out .= "<li>" . $i . "\n</li>";
} else {
$out .= "<li>" . $i . "\n</li>";
}
}
if ($page < ($tpages - $adjacents)) {
$out .= "<a style='font-size:11px' href=\"" . $reload . "&page=" . $tpages . "\">" . $tpages . "</a>\n";
}
// next
if ($page < $tpages) {
$out .= "<li>" . $nextlabel . " \n</li>";
} else {
$out .= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
}
$out .= "";
return $out;
}
Thanks in advance.

adding search filters in php

This is a search engine & pagination with one filter which is according to a word in "search", I want to add two more search filters: role & search between two dates.
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","");
mysql_select_db("page");
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each)
{$x=0;
$construct="";
$x++;
if($x==1)
$construct .="name LIKE '%$search_each%'";
}
$constructs ="SELECT * FROM info WHERE $construct";
$run = mysql_query($constructs);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "Sorry";
else
{
echo "$foundnum results found !<p>";
$per_page = 1;
$start = $_GET['start'];
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0;
$getquery = mysql_query("SELECT * FROM info WHERE $construct LIMIT $start, $per_page");
while($runrows = mysql_fetch_assoc($getquery))
{
$name = $runrows ['name'];
}
//Pagination Starts
echo "<center>";
$prev = $start - $per_page;
$next = $start + $per_page;
$adjacents = 3;
$last = $max_pages - 1;
if($max_pages > 1)
{
//previous button
if (!($start<=0))
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";
//pages
if ($max_pages < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
$i = 0;
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
elseif($max_pages > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a href='index.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//close to end; only hide early pages
else
{
echo " <a href='index.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
}
//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='index.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
}
echo "</center>";
}
?>
You should rewrite you loop like this
//by name
$construct="true";
foreach($search_exploded as $search_each) {
$construct .= " AND name LIKE '%$search_each%'";
}
//by role
if($role) {
$construct .= " AND role = '$role'";
}
//by date
if($start) {
$construct .= " AND the_date >= '$start'";
}
if($end) {
$construct .= " AND the_date <= '$end'";
}
Otherwise you constantly reset the values of $x (which is not necessary anyway) and $construct.
Of course you would have to GET the dates/role and format them properly.

Pagination System

I made this script for my website.
It works and everything but there is one small problem.
You can go forward infinitely
You can just click Next Page in an endless loop
And for some reason when i echo $num_pages I get 1 0_0
How can I fix the "infinie nexting" - my weird definition for the problem :)
<?php
$per_page = 4;
$start = 1;
if(!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
if($page <= 1) {
$start = 1;
$page = 1;
} else {
$start = $page * $per_page - $per_page;
}
$next = $page+1;
$previous = $page-1;
$GetAllComments = $con->query("SELECT * FROM comments LIMIT $start, $per_page");
$num_rows = $GetAllComments->num_rows;
$num_pages = $num_rows / $per_page;
while($GAC = $GetAllComments->fetch_object()) {
echo "<div class='well'> <h3>". $GAC->Title. "</h3>
". $GAC->Content. " <hr /> <em> Posted By ". $GAC->PosterName ." </em>
</div>";
}
$pagen = $page+1;
$pagep = $page-1;
echo "
<div class='pagination'>
<ul>
";
if($page > 1) {
echo "
<li><a href='?page=$previous'>".$pagep."</a> </li>
";
}
echo "
<li class='disabled'><a href='#'>$page</a></li>
";
echo "
<li> <a href='?page=$next'>" . $pagen . "</a></li>
</ul>
</div>
";
I would put the display in a for loop:
for($i = 1; $i <= $num_pages; $i++)
{
enter code here
}
That way it will repeat for as many pages are in num_pages and not anymore.

php pagination, something wrong

Some code from me.
$files = glob("pardod/*.html");
$record_count = 5;
$total_pages = ceil(count($files)/$record_count);
$page = $_GET['page'];
$offset = ($page-1)*$record_count;
$files_filter = array_slice($files, $offset,$record_count);
for ($i = 0; $i<$filecount; $i++){
if ($page){
$start = ($page - 1) * $record_count;
}else{
$start = 0;
}
}
if($total_pages > 1){
if($page != 1){
echo 'Atpakal';
}
if($page != $total_pages){
echo 'Uz priekšu';
}
}
The php pagination dont work, i am just learning how to make, where is a problem?
The *.html files didn't shows :(
Try this code for pagination
<?php
$con=mysql_connect("localhost","root","");
$page=$_REQUEST['page'];
if ($page < 1)
{
$page = 1;
}
$resultsPerPage =15;
$startResults = ($page - 1) * $resultsPerPage;
$numberOfRows = mysql_num_rows(mysql_query('SELECT * FROM tablename'));
$totalPages = ceil($numberOfRows / $resultsPerPage);
echo"<center><table border='1' bordercolor='blue' height='90%' width='90%'> <tr><th bgcolor='silver'>Name</th><th bgcolor='silver'>Password</th><th bgcolor='silver'>Question</th><th bgcolor='silver'> Answer</th><th bgcolor='silver'>Image</th> </tr>";
$i=1;
$result= mysql_query("SELECT * FROM password LIMIT $startResults, $resultsPerPage");
while($row=mysql_fetch_array($result))
{
}
echo"</tr></table></center>";
echo '<center>First&nbsp';
if($page > 1)
echo 'Back&nbsp';
for($i = 1; $i <= $totalPages; $i++)
{
if($i == $page)
echo '<strong>'.$i.'</strong>&nbsp';
else
echo ''.$i.'&nbsp';
}
if ($page < $totalPages)
echo 'Next ';
echo 'Last</center>';
?>
Try This Code
$limit = ( isset($_GET['limit'])) ? $_GET['limit'] : 5;
if (strtolower($limit) == 'all') {
$limit = 'all';
} else {
$limit = filter_var($limit, FILTER_SANITIZE_NUMBER_INT);
if (trim($limit) == '') {
$limit = 5;
}
}
$page = ( isset($_GET['page'])) ? $_GET['page'] : 1;
$page = filter_var($page, FILTER_SANITIZE_NUMBER_INT);
$links = ( isset($_GET['links'])) ? $_GET['links'] : 1;
$links = filter_var($links, FILTER_SANITIZE_NUMBER_INT);
Here is the link where step by step described all the process in details.
Simple Pagination in PHP By Learning Ocean Team
if you are using bootstrap, you can use the following
function pagination($page, $count) {
global $options;
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$actual_link = trim(str_replace("page=".$page, "", $actual_link), "&");
$lenght = ceil($count/$options->get("result_per_page"));
if ($page <= 3) {
if ($lenght < 5) {
$first = 0;
$last = $lenght-1;
} else {
$first = 0;
$last = 4;
}
} else if ($page < ($lenght-2)) {
$first = $page-2;
$last = $page+2;
} else if ($page <= $lenght) {
$first = $page-5;
$last = $lenght-1;
}
if ($page > 0) {
$prev = $page-1;
$next = $page+1;
} else {
$prev = 0;
$next = $page+1;
}
echo '<span>Viewing page '.($page+1).' of '.$lenght.'</span><br>';
echo '<nav aria-label="Page navigation example">';
echo '<ul class="pagination justify-content-center">';
if ($prev > 0) {
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page=0">««</a></li>';
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.$prev.'">«</a></li>';
}
for ($i = $first; $i<=$last; $i++) {
if ($i == $page) {
$active = ' active';
} else {
$active = '';
}
echo '<li class="page-item'.$active.'"><a class="page-link" href="'.$actual_link.'&page='.$i.'">'.($i+1).'</a></li>';
}
if ($next < $last) {
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.$next.'">»</a></li>';
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.($lenght-1).'">»»</a></li>';
}
echo '</ul>';
echo '</nav>';
}
to call the function
$pageNumber = 1;
$totalRowCount = 5000;
pagination($pageNumber, $totalRowCount);

Categories