Jump to content

Recommended Posts

Posted

Hi

I need to have a search form linked to my news articles. The fields I need are shown in the attachment

post-761-0-05835300-1354268783_thumb.jpg

Is this possible in processwire? Could someone give me pointers on how to go about this?

Many thanks!

Posted

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>";
}

  • Like 3

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.
×
×
  • Create New...