Fahd Sana Ullah
Fahd Sana Ullah

Reputation: 1

Keeping GET variables in pagination

I have around 10 GET variables in an HTML form.

These are passes onto search.php, which returns 20 results per page via pagination. However, when I click the "next" page button, it rewrites the url as "?pageno=2". It forgets all the GET variables in the first place.

What's the simplest way I can keep these variables into the next page? I can NOT use Session because I need the user to be open as many instances of the search system as possible, and I cant edit the URL manually because the URL may have 2 variables or 14 variables, depending on what the user selects.

Upvotes: 0

Views: 584

Answers (2)

Fahd Sana Ullah
Fahd Sana Ullah

Reputation: 1

Although http_build_query is the common method used, as mentioned by fubar, I found a simpler solution.

I used preg_replace to just change the pageno in the URL

     <!-- Next li -->

<li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
<a href="<?php if($pageno >= $total_pages){ echo '#'; } else { 

$npage = "&pageno=". (intval($pageno) + intval(1));

if (isset($_GET['pageno']))

 {

$npg = preg_replace('~((?<=\?)pageno=\d+&?|&pageno=\d+)~i', $npage, $_SERVER['REQUEST_URI']);               

echo $npg;

 }

else 

 { 

echo $_SERVER['REQUEST_URI']. $npage; 

 }

 } ?>">Next</a>
</li>

The variable $pageno is set in the beginning of the pagination. If it's found null in $_GET['pageno'], it is set to 1. Otherwise it takes its GET value.

Upvotes: 0

fubar
fubar

Reputation: 17388

You could use the $_GET array, and http_build_query() function. E.g.

$params = $_GET;
$params['page'] = $page;  // Set previous/next page

$uri = $_SERVER['REQUEST_URI'];
$query = http_build_query($params);

$url = "{$uri}?{$query}";

Upvotes: 1

Related Questions