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?
Related
I have OpenCart 2.1 and the website has such pagination (see screenshot)
default pagination
Here is the question: How to make like a product diapason pagination in OpenCart?
When I say the diapason pagination, I mean this (see screenshot please)
diapason pagination screenshot
Here is some code that is responsible for the formation of pagination.
<?php
class Pagination {
public $total = 0;
public $page = 1;
public $limit = 20;
public $num_links = 8;
public $url = '';
public $text_first = '|<';
public $text_last = '>|';
public $text_next = '>';
public $text_prev = '<';
public function render() {
$total = $this->total;
if ($this->page < 1) {
$page = 1;
} else {
$page = $this->page;
}
if (!(int)$this->limit) {
$limit = 10;
} else {
$limit = $this->limit;
}
$num_links = $this->num_links;
$num_pages = ceil($total / $limit);
$this->url = str_replace('%7Bpage%7D', '{page}', $this->url);
$output = '<ul class="pagination">';
if ($page > 1) {
$output .= '<li>' . $this->text_first . '</li>';
$output .= '<li>' . $this->text_prev . '</li>';
}
if ($num_pages > 1) {
if ($num_pages <= $num_links) {
$start = 1;
$end = $num_pages;
} else {
$start = $page - floor($num_links / 2);
$end = $page + floor($num_links / 2);
if ($start < 1) {
$end += abs($start) + 1;
$start = 1;
}
if ($end > $num_pages) {
$start -= ($end - $num_pages);
$end = $num_pages;
}
}
for ($i = $start; $i <= $end; $i++) {
if ($page == $i) {
$output .= '<li class="active"><span>' . $i . '</span></li>';
} else {
$output .= '<li>' . $i . '</li>';
}
}
}
if ($page < $num_pages) {
$output .= '<li>' . $this->text_next . '</li>';
$output .= '<li>' . $this->text_last . '</li>';
}
$output .= '</ul>';
if ($num_pages > 1) {
return $output;
} else {
return '';
}
}
}
I have my website that I converted to bootstrap and all is well but pagers. The system my site uses now in functions.php
function pager($rpp, $count, $href, $opts = array())
{
global $language, $btit_settings;
$pager_type = "new";
if($btit_settings["fmhack_pager_type_select"] == "enabled")
$pager_type = $btit_settings["pager_type"];
if($pager_type == "new")
{
$pages = ($rpp == 0)?1:ceil($count / $rpp);
if(!isset($opts['lastpagedefault']))
$pagedefault = 1;
else
{
$pagedefault = floor(($count - 1) / $rpp);
if($pagedefault < 1)
$pagedefault = 1;
}
$pagename = 'pages';
if(isset($opts['pagename']))
{
$pagename = $opts['pagename'];
if(isset($_GET[$opts['pagename']]))
$page = max(1, intval($_GET[$opts['pagename']]));
else
$page = $pagedefault;
}
elseif(isset($_GET['pages']))
{
$page = max(1, intval(0 + $_GET['pages']));
if($page < 0)
$page = $pagedefault;
}
else
$page = $pagedefault;
$pager = '';
if($pages > 1)
{
$pager .= "\n".'<form name="change_page'.$pagename.'" method="post" action="index.php">'."\n".'<select class="drop_pager" name="pages" onchange="location=document.change_page'.$pagename.
'.pages.options[document.change_page'.$pagename.'.pages.selectedIndex].value" size="1">';
for($i = 1; $i <= $pages; $i++)
$pager .= "\n<option ".($i == $page?'selected="selected"':'')."value=\"$href$pagename=$i\">$i</option>";
$pager .= "\n</select>";
}
$mp = $pages; // - 1;
$begin = ($page > 3?($page < $pages - 2?$page - 2:$pages - 2):1);
$end = ($pages > $begin + 2?($begin + 2 < $pages?$begin + 2:$pages):$pages);
if($page > 1)
{
$pager .= "\n <ul class=\"pagination\"><li> «</li>></ul>";
$pager .= "\n<ul class=\"pagination\"><li>< </li></ul>";
}
if($count)
{
for($i = $begin; $i <= $end; $i++)
{
if($i != $page)
$pager .= "\n <ul class=\"pagination\"><li>$i</li></ul>";
else
$pager .= "\n <li class=\"active\">$i</li>";
}
if($page < $mp && $mp >= 1)
{
$pager .= "\n <ul class=\"pagination\"><li> ></li></ul>";
$pager .= "\n <ul class=\"pagination\"><li> »</li></ul>";
}
$pagertop = "$pager\n</form>";
$pagerbottom = str_replace("change_page", "change_page1", $pagertop)."\n";
}
else
{
$pagertop = "$pager\n</form>";
$pagerbottom = str_replace("change_page", "change_page1", $pagertop)."\n";
}
$start = ($page - 1) * $rpp;
if($pages < 2)
{
// only 1 page??? don't need pager ;)
$pagertop = '';
$pagerbottom = '';
}
return array(
$pagertop,
$pagerbottom,
"LIMIT $start,$rpp");
}
Is there a easy way or plugin i can use to incorporate bootstraps?
Here is sample from a page requests.php
list($pagertop,$pagerbottom,$limit) = pager(intval($btit_settings['req_page']),$count,$dir);
then sets it to a tag
$requeststpl->set("bottom_pager",$pagerbottom);
$requeststpl->set("top_pager",$pagertop);
which is then used as a tag to display it via the requests.tpl
<tag:bottom_pager />
<tag:top_pager />
I just want to have it so it looks like bootstrap style..... but comes out like this....
SAMPLE
SAMPLE2
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 :)
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';
}
}?>
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";
}
}