Jump to content

Set result limit using Get variable

Recommended Posts

Hi everybody,

I have got a (hopefully) pretty simple question but I can't find a "clean" solution for it and hope you can help me.

I'm using a form to let the user specify different variables (Get Request) and show them the according pages.

It's more or less similar to the Skyscraper Demo page.

Now I'd like to add three links to set the limit of my query. So if the user clicks on e.g. 10  -> only 10 pages should be listed (leaving the other variables the same).

I'd like to generate links with the current URL but change the limit variable.

Is there a function to get the current URL with all the get parameters and let me change/add one of them?

Or do you have any other suggestions how I could add this functionality?

Thanks for your help!

Share this post

Link to post
Share on other sites

Do you want to do this?

//.. build your normal selector...
if ($input->get->limit) {
  $limit = (int) $input->get->limit;
  $selector .= ", limit=$limit";

How to use limit in your selectors:


Share this post

Link to post
Share on other sites

I wanted to know if there is an easy way to generate the URLs for my links.

I'm now using plain PHP functionality:

$params = array_merge($_GET, array("limit" => 10));
$new_query_string = http_build_query($params);

Thanks for your help!

Share this post

Link to post
Share on other sites

Since you are pulling them right out of $_GET (or $input->get), you should filter and sanitize them before doing your http_build_query.

$types = array(
  'limit' => 0,
  'my_string_var' => '',
  'my_int_var' => 0, 

$params = array();

foreach($types as $name => $type) {
  $value = $input->get->$name; 
  if($value === null) continue; // not specified, skip over
  if(is_string($type)) $params[$name] = $sanitizer->text($value);  // sanitize as string
    else $params[$name] = (int) $value; // sanitizer as int

if(empty($params['limit'])) $params['limit'] = 10; // set a default

$new_query_string = http_build_query($params); 
  • Like 3

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Greg Lumley
      Hi! In busy learning to know PW better I'm looking at existing code.
      I have Bitpoets Editorial Responsive Blog as example. While looking through the code I've just found this line in the blog-head.php 
      <link rel="stylesheet" href="<?= $config->urls->templates ?>assets/css/main.css?ts=<?= time() ?>" /> I hope it's not a stupid question but I've never seen this before...
      main.css?ts=<?= time() ?> I notice the main template is empty but of course renders on the front end.
      Using a timestamp really has me totally confused. Can someone explain it to me please. 
      Thank you! 
    • By jds43
      I have a search page loosely based on Skyscrapers where I'm parsing a selector with options 'beds', 'bathrooms', 'size' fields. It is working well until I select 'Any' after I've run a search. This is where no results are returned (/?beds=&bathrooms=&size=&submit=). I want it to reset and show all results.
      I hope this isn't too vague.
    • By brdje
      I've created a product database which all use the template product.php and are published on the website.
      I'm trying to create an overview table with pagination of all the products, but using $pages->get does not return an object, only the object title.
      $products = $pages->get("template=product, limit=10"); This returns 10 strings in the frontend but not the object. When looping over the result set to get product fields, i get the following error:
      Notice: Trying to get property 'title' of non-object
      Because the product database is fairly large, i've added a pagination using the same query and this does return all the pages, but i can't click on the links that the paginator renders.
      $results = $pages->find("template=product, limit=10"); if($results->getTotal() > 10) { echo $results->renderPager(array( "nextItemLabel" => "Volgende", "previousItemLabel" => "Vorige", "currentItemClass" => 'active' )); }
      I've added the option in the template to allow page numbers. When i navigate to the link manually, i still end up seeing the 10 first products.
      I've tried changing several settings around, but i'm a bit stuck on how to resolve these issues. Do you have any advice what I need to adjust?
      Thanks in advance for your feedback.
    • By Peter Troeger
      Hello Community 🙂
      Has anyone ever tried having multiple elements on one page that get their info with $pages->find('selector, limit=n') and tried using pagination on one of these elements without effecting the other?
      I have a slider on a page where I display content with pagination. But when I go to page two, the slider content also goes to page two, which I don't want it to do 🙂
      Any tips are greatly appreciated!
    • By pwFoo
      With JS fetch() it is a bit tricky to get for example $_POST populated (params need to be FormData / forms), but would it possible to get PW $input (get, post, urlSegments, queryString) working for fetch requests?
      Is there a fix / hook / workaround to solve it PW side?
  • Create New...