pagination with hash and parameters doesn't work - php

I have page with a URL like this:
http://***.com/profile/username#profile_favs
With my pagination it looks like this:
http://***.com/profile/username?s=0&p=1#profile_favs
The last example doesn't work.
Basically my pagination-function looks like this:
function Pagination($pages, $start, $display, $link_url="", $anchor="") {
echo '<div id="pagination">';
$current_page = ($start / $display) + 1;
$paginator_num = 5;
$pages_display = 10;
if ($current_page > $pages - $paginator_num) {
$paginator_num = $pages_display - ($pages - $current_page);
} elseif ($current_page < $paginator_num + 1) {
$paginator_num = $pages_display - $current_page;
} else {
$paginator_num = 5;
}
$min = max($current_page - $paginator_num, 1);
$max = min($current_page + $paginator_num, $pages);
for ($i = $min; $i <= $max; $i++) {
if ($i != $current_page) {
echo '<div class="pagination_link">';
echo '' . $i . '';
echo '</div>';
} else {
echo '<div id="pagination_active" class="pagination_link">';
echo $i . ' ';
echo '</div>';
}
}
echo '</div>';
}
And this is the part that calculates the pages (this part gets included right before my pagination-function):
<?php
$display = $display_num;
if (isset($_GET['p']) && is_numeric ($_GET['p'])) {
$pages = $_GET['p'];
} else {
$total_results = $qr_num;
if ($total_results > $display) {
$pages = ceil ($total_results / $display);
} else {
$pages = 1;
}
}
if (isset($_GET['s']) && is_numeric ($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
?>
Now my question is: Is there any workaround possible to get that pagination working with a URL mentioned above:
http://***.com/profile/username?s=0&p=1#profile_favs
Actually I'd prefer a solution without GET-parameters.

You could use an AJAX call with the XMPHttpRequest Object to fetch it without the get parameters.
Another thing you could do (if you are using Apache) is to look at mod_rewrite for urls.
Hope that helps :)

Related

Pagination redirecting to home page

I am not a coder by any means, just dabbling with PHP. On our website we have this function:
public static function mypagination($page, $result)
{
$totalPages = ceil($result / 100);
$links = "";
$links .= "<a href='/index.php/flightsearch/findflights?p=1'><div class='flightsearch-pagination-link'>First</div></a>";
for ($i = 1; $i <= $totalPages; $i++) { $linkpage = $i; $curpage = $page;
if($i != $curpage) {
if($i < ($curpage + 10) && $i > ($curpage - 10))
{
$links .= "<a href='/index.php/flightsearch/findflights?p=$linkpage'><div class='flightsearch-pagination-link'>$i</div></a>";
}
}
else
{
$links .= "<div class='flightsearch-pagination-link flightsearch-pagination-active'>$curpage</div>";
}
}
$links .= "<a href='/index.php/flightsearch/findflights?p=$totalPages'><div class='flightsearch-pagination-link'>Last</div></a>";
return $links;
}
}
?>
When I call the function on another template the pagination does not work and redirects me to the homepage. We have no htt.access file active.
Could anyone please point me in the right direction?

PHP remove page 0 from pagination

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);

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';
}
}?>

Href format using master page php

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.

PHP & MySQL pagination display problem

I asked a similar question like this yesterday but after waiting for ever I figured out part of the problem but now I'm stuck again I'm trying to display ... when the search results are to long because my pagination links will keep on displaying and will not stop until every link is displayed on the page.
For example I'm trying to achieve the following in the example below. Can some one help me fix my code so I can update my site. Thanks
This is what I want to be able to do.
First Previous 1 2 ... 5 6 7 8 9 10 11 12 13 ... 199 200 Next Last
Here is my pagination code that displays the links.
$display = 20;
if (isset($_GET['p']) && is_numeric($_GET['p'])) {
$pages = $_GET['p'];
} else {
$q = "SELECT COUNT(id) FROM comments WHERE user_id=3";
$r = mysqli_query ($mysqli, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($mysqli));
$row = mysqli_fetch_array ($r, MYSQLI_NUM);
$records = $row[0];
if ($records > $display) {
$pages = ceil ($records/$display);
} else {
$pages = 1;
}
}
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
//content goes here
if ($pages > 1) {
echo '<br /><p>';
$current_page = ($start/$display) + 1;
if ($current_page != 1) {
echo 'First';
}
if ($current_page != 1) {
echo 'Previous ';
}
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
echo '' . $i . ' ';
} else {
echo '<span>' . $i . '</span> ';
}
}
if ($current_page != $pages) {
echo 'Next';
}
if ($current_page != $pages) {
echo 'Last';
}
echo '</p>';
}
Instead of the loop just use something like this:
if($current_page > 8 && $pages > 11) {
echo '1 ';
echo '2 ';
echo '...';
}
for ($i = max(1, $current_page - 4); $i < min($current_page + 4, $pages); $i ++) {
echo '' . $i . ' ';
}
if ($current_page < $pages - 8 && $pages > 11) {
echo '...';
echo '' . ($pages - 1) . ' ';
echo '' . $pages . ' ';
}

Categories