Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example filter form with pagination
<?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
You can’t perform that action at this time.