How to customize the pagination page number below?
Current:
and what I want is:
1 2 3 4 5 Next Last Page
Here is the PHP code:
for($i=1; $i<=$Num_Pages; $i++)
{
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
Any clue how to do this?
Could try this:
$Num_Pages = 70;
//$Page = $_GET['Page'];
$Page = 12;
$from = $Page - ($Page % 5) + 1;
for($i=$from; $i<$from+5 && $i<$Num_Pages; $i++)
{
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
$next_page = $Page+1;
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$next_page '>next</a> ]";
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$Num_Pages'>Last Page</a> ]";
Replace the $Page = 12 with the $_GET to get the page dynamically.
Haven't really tested it.
$start_page = intval($Page / 5) *5 + 1;
$to_page = $start_page + 4;
$is_last_group = ($start_page + 5)> $Num_Pages);
if (is_last_group)
$to_page = $Num_Pages;
for($i = $start_page; $i <= to_page; $i++)
{
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
if($page < $Num_Pages){
$next_page = $page + 1;
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$next_page '>next</a> ]";
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$Num_Pages'>last</a> ]";
}
Related
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
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>";
My pagination is showing a page zero (0) when you go to page 2. Not sure why. I don't want to show a page zero.
I'll try to only show the necessary code.
Here is my code:
<?php
$rec_limit = 100;
$targetpage = "dispatch.php";
if (isset($_GET['page']))
{
$page = $_GET['page'];
$offset = $rec_limit * ($page - 1);
}
else
{
$page = 1;
$offset = 0;
}
*** $left_rec = countRecords() - ($page * $rec_limit); ***
$total_records = countRecords(); // countRecords() should be self-explanatory
$total_pages = ceil($total_records / $rec_limit); // $rec_limit is 100
$adjacents = 2;
$previousPage = $page - 1;
$nextPage = $page + 1;
$querystring = "";
$start = ($page < $adjacents ? 1 : $page - $adjacents); // <-- i think the issue is here
$beginning = 1;
$end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);
foreach ($_GET as $key => $value)
{
if($key != "page") $querystring .= "$key=$value&";
}
echo "<div class="row-fluid"><div class="span2"><ul class="pager"><li>First</li>";
if ($left_rec < $rec_limit)
{
$last = $page - 1;
echo #"<li>Previous</li>";
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
}
else if($page == 0)
{
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
}
else if ($page > 0)
{
$last = $page - 2;
echo "<li>Previous</li> ";
for($i= $start; $i <= $end; $i++)
{
echo #"<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
}
echo "<li>Last</li>";
echo '</ul></div></div>';
?>
I would really appreciate the help in removing page 0 from the application. Please disregard any typos or missing quotes. The code works with the exception of it showing page 0.
I added a picture of what the application showing page 0. It only shows page 0 when I go to page 2. After that, I no longer see page 0.
Please let me know what I have to do.
Thanks.
Some advice:
You really shouldn't suppress errors using #, instead you should be instantiating all of your variables and writing proper code.
Don't hard-code pagination into each page. Instead, wrap it in a reusable function.
Example:
// draws a menu for navigating multiple pages of content
function paginate($page, $display, $total) {
if(isset($_SERVER['QUERY_STRING']) && trim($_SERVER['QUERY_STRING']) != '') {
if(stristr($_SERVER['QUERY_STRING'], 'page=')) {
$query = '?' . preg_replace('/page=\d+/', 'page=', $_SERVER['QUERY_STRING']);
} else {
$query = '?' . $_SERVER['QUERY_STRING'] . '&page=';
}
} else {
$query = '?page=';
}
$pages = $total <= $display ? 1 : ceil($total / $display);
$self = htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'utf-8');
$first = 'first';
$prev = 'prev';
$next = 'next';
$last = 'last';
echo '<p>';
echo ($page > 1) ? "$first | $prev |" : 'first | prev |';
echo '(page ' . $page . ' of ' . $pages . ')';
echo ($page < $pages) ? "| $next | $last" : '| next | last';
echo '</p>';
}
// output example
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$display = 100;
$start = $display * $page - $display;
$total = countRecords($start, $display);
paginate($page, $display, $total);
I agree with #mister martin, but if you use this code fundamentally, try to change
$start = ($page < $adjacents ? 1 : $page - $adjacents);
To:
$start = ($page < $adjacents ? $page : $page - $adjacents);
Edited: maybe problem in undefined $left_rec, check demo. You can change $s like $_GET['page'] and as I see all works correctly.
Demo.
<?php
$rec_limit = 100;
$targetpage = "dispatch.php";
$total_records = countRecords();
// Nav part
$page = intval($_GET['page'])? $_GET['page']: 1;
$offset = $rec_limit * ($page - 1);
$total_pages = ceil($total_records / $rec_limit); // $rec_limit is 100
$adjacents = 2;
$previousPage = $page - 1;
$nextPage = $page + 1;
$start = ($page < $adjacents ? $page : $page - $adjacents); // <-- I think the issue is this line
$beginning = 1;
$end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);
$uri = $_GET;
unset($uri['page']);
$querystring = http_build_query($uri);
echo '<div class="row-fluid"><div class="span2"><ul class="pager"><li>First</li>';
if($left_rec < $rec_limit && ($page > 1))
{
echo "<li>Previous</li>";
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
} else if ($page > 1)
{
echo "<li>Previous</li> ";
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
} else {
for($i= $start; $i <= $end; $i++)
{
echo "<li " . ((($page)==$i)? "class=\"active\"" : "") . ">$i</li>";
}
echo "<li>Next</li>";
}
echo "<li>Last</li>";
echo '</ul></div></div>';
?>
I was able to get my pagination script working, and everything works fine except when you go to PAGE 2, you can see a PAGE 0, which if you click on it give you nothing.
I need to fix my current code so that the application does not show a PAGE 0.
Here is the necessary code:
<?php
$total_records = countRecords(); // self explanatory function called here
$total_pages = ceil($total_records / $rec_limit);
$adjacents = 2;
$previousPage = $page - 1;
$nextPage = $page + 1;
$querystring = "";
$start = ($page < $adjacents ? 1 : $page - $adjacents);
$beginning = 1;
$end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);
foreach($_GET as $key => $value)
{
if($key != "page") $querystring .= "$key=$value&";
}
echo
'<div class="row-fluid">
<div class="span2">'.countRecords()." total records" .'</div>
<div class="container pagination-small">
<ul style="margin: 3px;" class="pager">';
echo #"<li>First</li>";
if ($left_rec < $rec_limit)
{
$last = $page - 1;
echo #"<li><a href=\"$targetpage?page=$previousPage&$querystring\">
Previous</a></li>";
for($i= $start; $i <= $end; $i++)
{
echo #"<li " . ((($page)==$i)? "class=\"active\"" : "") . ">
$i</li>";
}
}
else if($page == 0)
{
for($i= $start; $i <= $end; $i++)
{
echo #"<li " . ((($page)==$i)? "class=\"active\"" : "") . ">
$i</li>";
}
echo #"<li>Next</li>";
}
else if ($page > 0)
{
$last = $page - 2;
echo #"<li><a href=\"$targetpage?page=$previousPage&$querystring\">
Previous</a></li>";
for($i= $start; $i <= $end; $i++)
{
echo #"<li " . ((($page)==$i)? "class=\"active\"" : "") . ">
$i</li>";
}
echo #"<li>Next<li>";
}
echo #"<li>Last</li>";
echo '<ul></div></div>';
I tried to keep the code as short as possible. If there are any errors, just note it's only a typo as I typed it here. The code works, with exception to my problem.
So with that all said, can anyone tell me how to remove PAGE 0 from the pagination? I have done some research, but I have been unsuccessful applying it to my code. So I'm hoping someone can take a look at my code and tell me how I can alter it to make this work.
I appreciate the help.
Thank you in advance.
Logic flaw:
$start = ($page < $adjacents ? 1 : $page - $adjacents);
If you're on Page 1, you'll get
$start = (1 < 2 ? 1 : 1 - 2);
$start = -1; // page negative one? huh?
Then this loop is pointless:
foreach($_GET as $key => $value) { ... }
Why not just
unset($_GET['page']);
$q = http_build_query($_GET);
I saw a similar post on stackoverflow but it failed to anwser my question.
I am using pagination to display results and the link is coming up broken when I add the master links. Most likely as I am incorrectly using the correct format.
Have tried a few formats to try to get it working based on other posts
<a href='../admin/admin.master.php?page=list_products.php&page=' .$j. ' id='page_a_link'>Next</a></span>
or
../admin/admin.master.php?page=list_products.php&page=$j
but neither work.
PHP PAGINATION SHOWING PAGE NUMBERS
<?php
if(isset($page))
{
$result = mysql_query("SELECT COUNT(*) As Total FROM products");
$rows = mysql_num_rows($result);
if($rows)
{
$rs = mysql_fetch_array($result);
$total = $rs["Total"];
}
$totalPages = ceil($total / $perpage);
if($page <=1 )
{
echo "<span id='page_links' style='font-weight:bold;'>Pre</span>";
}
else
{
$j = $page - 1;
echo "<span><a id='page_a_link' href='../admin/admin.master.php?page=list_products.php&page=$j'>< Pre</a></span>";
}
for($i=1; $i <= $totalPages; $i++)
{
if($i<>$page)
{
echo "<span><a href='../admin/admin.master.php?page=list_products.php&page=' .$i. ' id='page_a_link'>$i</a></span>";
}
else
{
echo "<span id='page_links' style='font-weight:bold;'>$i</span>";
}
}
if($page == $totalPages )
{
echo "<span id='page_links' style='font-weight:bold;'>Next ></span>";
}
else
{
$j = $page + 1;
echo "<span><a href='../admin/admin.master.php?page=list_products.php?page=' .$j. ' id='page_a_link'>Next</a></span>";
}
}
?>
You are mixing double quotes with single quotes, that's why it breaks. Try this:
<?php
if(isset($page))
{
$result = mysql_query("SELECT COUNT(*) As Total FROM products");
$rows = mysql_num_rows($result);
if($rows)
{
$rs = mysql_fetch_array($result);
$total = $rs["Total"];
}
$totalPages = ceil($total / $perpage);
if($page <=1 )
{
echo '<span id="page_links" style="font-weight:bold;">Pre</span>';
}
else
{
$j = $page - 1;
echo '<span><a id="page_a_link" href="../admin/admin.master.php?page=list_products.php&page=' . $j . '">< Pre</a></span>';
}
for($i=1; $i <= $totalPages; $i++)
{
if($i<>$page)
{
echo '<span>' . $i . '</span>';
}
else
{
echo '<span id="page_links" style="font-weight:bold;">' . $i . '</span>';
}
}
if($page == $totalPages )
{
echo '<span id="page_links" style="font-weight:bold;">Next ></span>';
}
else
{
$j = $page + 1;
echo '<span>Next</span>';
}
}
?>
Also, the HTML code you generate isn't valid, as you assign the same ID to multiple elements. Consider changing id="page_links" and id="page_a_link" to class="page_link" and class="page_a_link" respectively, then change #page_links and #page_a_link in your CSS to .page_links and .page_a_link
You are assigning two values to page in the URL '../admin/admin.master.php?**page**=list_products.php&**page**=' .$j. ' try replacing page = $j with page_num = $j or something of your choice and replace it in the and in the code.