Created
July 5, 2018 13:21
Star
You must be signed in to star a gist
Example filter form with pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace ProcessWire; | |
$filter = ""; | |
$form = $modules->InputfieldForm; | |
$form->attr("action", $page->url); | |
$form->attr("method", "get"); | |
// select with sort options | |
$f = $modules->InputfieldSelect; | |
$f->attr("name", "sort"); | |
$f->label = "Sort"; | |
$f->addOptions(array( | |
"-title" => "descending", | |
"title" => "ascending" | |
)); | |
$form->add($f); | |
// submit button | |
$f = $modules->InputfieldSubmit; | |
$f->attr("name", "filter"); | |
$form->add($f); | |
// process form | |
if(count($input->get)){ | |
// process form input and populate fields | |
$form->processInput($input->get); | |
// if sort is not empty | |
if($form->get("sort")->value) { | |
// build filter and add to whitelist so it gets picked up by pagination | |
$input->whitelist("sort", $sanitizer->text($form->get("sort")->value)); | |
$filter .= ",sort=" . $input->whitelist("sort"); | |
} | |
} | |
$result = $pages->find("template=basic-page, limit=2{$filter}"); | |
$content .= "<h2>Show Results</h2>"; | |
if(!$result->count) { | |
$content .= "<p>no results</p>"; | |
} else { | |
foreach($result as $res){ | |
$content .= "<p>$res->title<br>$res->url</p>"; | |
} | |
$content .= $result->renderPager(); | |
} | |
$content .= $form->render(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment