0

I've implemented pagination system for my website in php, only problem with that is following:
suppose my url at which pagination present is,www.abc.com/search.php?mode=study&query=a,currently its showing 20 results per page,when I use pagination to navigate across results, the url changes to www.abc.com/search.php?mode=study&query=a&page=2,www.abc.com/search.php?mode=study&query=a&page=2&page=3 and www.abc.com/search.php?mode=study&query=a&page=2&page=3&page=4,so the problem which I'm facing is when I navigate across results,every time page variable got appended to the url,which is really annoying.
My code for pagination is.

<?php
function displayPaginationBelow($per_page,$page,$total){

$page_url="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&";
$adjacents = "2";
$page = ($page == 0 ? 1 : $page); 
$start = ($page - 1) * $per_page;                              
$prev = $page - 1;                         
$next = $page + 1;
$setLastpage = ceil($total/$per_page);
$lpm1 = $setLastpage - 1;
$setPaginate = "";
if($setLastpage > 1)
{  
    $setPaginate .= "<ul class='setPaginate'>";
    $setPaginate .= "<li class='setPage'>Page $page of $setLastpage</li>";
    if ($setLastpage < 7 + ($adjacents * 2))
    {  
        for ($counter = 1; $counter <= $setLastpage; $counter++)
        {
            if ($counter == $page)
                $setPaginate.= "<li><a class='current_page'>$counter</a></li>";
            elseif (isset($_GET['page'])) {
                unset($_GET['page']);
                $page_url = http_build_query($_GET);
                echo $page_url;
                $setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>";
            }
            else
                $setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>";                 
        }
    }
    elseif($setLastpage > 5 + ($adjacents * 2))
    {
        if($page < 1 + ($adjacents * 2))    
        {
            for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
            {
                if ($counter == $page)
                    $setPaginate.= "<li><a class='current_page'>$counter</a></li>";
                else
                    $setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>";                 
            }
            $setPaginate.= "<li class='dot'>...</li>";
            $setPaginate.= "<li><a href='{$page_url}page=$lpm1'>$lpm1</a></li>";
            $setPaginate.= "<li><a href='{$page_url}page=$setLastpage'>$setLastpage</a></li>";     
        }
        elseif($setLastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
        {
            $setPaginate.= "<li><a href='{$page_url}page=1'>1</a></li>";
            $setPaginate.= "<li><a href='{$page_url}page=2'>2</a></li>";
            $setPaginate.= "<li class='dot'>...</li>";
            for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
            {
                if ($counter == $page)
                    $setPaginate.= "<li><a class='current_page'>$counter</a></li>";
                else
                    $setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>";                 
            }
            $setPaginate.= "<li class='dot'>..</li>";
            $setPaginate.= "<li><a href='{$page_url}page=$lpm1'>$lpm1</a></li>";
            $setPaginate.= "<li><a href='{$page_url}page=$setLastpage'>$setLastpage</a></li>";     
        }
        else
        {
            $setPaginate.= "<li><a href='{$page_url}page=1'>1</a></li>";
            $setPaginate.= "<li><a href='{$page_url}page=2'>2</a></li>";
            $setPaginate.= "<li class='dot'>..</li>";
            for ($counter = $setLastpage - (2 + ($adjacents * 2)); $counter <= $setLastpage; $counter++)
            {
                if ($counter == $page)
                    $setPaginate.= "<li><a class='current_page'>$counter</a></li>";
                else
                    $setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>";                 
            }
        }
    }
    if ($page < $counter - 1){
        $setPaginate.= "<li><a href='{$page_url}page=$next'>Next</a></li>";
        $setPaginate.= "<li><a href='{$page_url}page=$setLastpage'>Last</a></li>";
    }else{
        $setPaginate.= "<li><a class='current_page'>Next</a></li>";
        $setPaginate.= "<li><a class='current_page'>Last</a></li>";
    }
    $setPaginate.= "</ul>\n";    
}
return $setPaginate;
}

I know problem is with $page_url variable, need help with this little problem

Magnotta
  • 933
  • 11
  • 34

2 Answers2

1
$page_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; //Create the url
$page_url = preg_replace("/(page\=[0-9]+[\&]*)/", "", $page_url); // Delete page query string.
if($page_url[strlen($page_url) - 1 ] !== "&"){ // If last char is not &
    $page_url .= "&"; // add &
}
aprogrammer
  • 1,764
  • 1
  • 10
  • 20
0

you need to strip off the old page param before adding the new one. So delete the param via unset()

In this answer a way to do this is shown.

Community
  • 1
  • 1
  • I tried `isset($_GET['page'])) { unset($_GET['page']); $page_url = http_build_query($_GET); echo $page_url;`,but its not working – Magnotta Jan 09 '17 at 09:20
  • you needed to parse the url into an array and extract the query portion before unseting it. But the preg_replace Erol KESKİN suggested works also. – elysian-design Jan 09 '17 at 09:28