Jump to content

Search Articles


gnome
 Share

Recommended Posts

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...