gnome Posted November 30, 2012 Share Posted November 30, 2012 Hi I need to have a search form linked to my news articles. The fields I need are shown in the attachment Is this possible in processwire? Could someone give me pointers on how to go about this? Many thanks! Link to comment Share on other sites More sharing options...
Exceptionz Project Posted November 30, 2012 Share Posted November 30, 2012 It is possibile with PW. You just need the user input from the select list and use the selectors listed here to code your own formular processing for customized search form: http://processwire.com/api/selectors/ Link to comment Share on other sites More sharing options...
Martijn Geerts Posted November 30, 2012 Share Posted November 30, 2012 A basic search is included in a clean ProcessWire install. You just have to expand the options there. Link to comment Share on other sites More sharing options...
ryan Posted December 2, 2012 Share Posted December 2, 2012 Once you've got your form, I think it'll be fairly easy to develop this. But your date fields are the only thing that's going to need a little extra work. Since you are asking for just month and year (no day) you'll want to qualify that to the beginning of the first month and the end of the second month. Finding the beginning of the first month is easy since all months start at day 1. There are various ways to find the end of the second month, but there's one way in my example below. Here's how I might perform the query from the API side (written in the browser so may contain typos): // where we will store our $pages->find selector $selector = array(); // add the search query if present if($input->get->search) { $search = $sanitizer->selectorValue($input->get->search); $selector[] = "title|body~=$search"; } if($input->get->yearFrom && $input->get->monthFrom) { // construct date like 2012-12-02 and convert to timestamp $dateFrom = strtotime("{$input->get->yearFrom}-{$input->get->monthFrom}-01 00:00:00"); if($dateFrom) $selector[] = "date>=$dateFrom"; } if($input->get->yearTo && $input->get->monthTo) { // do the same for month-to, but find the beginning $dateTo = strtotime("{$input->get->yearTo}-{$input->get->monthTo}-01 00:00:00"); // now find the end of dateTo month by adding 1 month and subtracting 1 second if($dateTo) $dateTo = strtotime("+1 MONTH", $dateTo) - 1; if($dateTo) $selector[] = "date<=$dateTo"; } // determine and validate the max records to show $limit = (int) $input->get->limit; if($limit > 50 || $limit < 1) $limit = 10; $selector[] = "limit=$limit"; // specify the template we are limiting the search to $selector[] = "template=article"; if(count($selector)) { // perform the search $selectorString = implode(', ', $selector); $articles = $pages->find($selectorString); echo "<h2>Found " . $articles->getTotal() . " articles matching your query.</h2>"; echo $articles->render(); // or however you want to output them } else { echo "<p>No search specified.</p>"; } 3 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now