Hi Tamara,
This won't be perfect and probably contains some typos but it's similar to something I implemented recently so maybe you can adapt it to suit your needs. It assumes your DO class is called Camcorder and has a has_one to Type of which there are two options - (1)New and (2)Used. You could create a separate template for search results by duplicating the relevant template and appending '_search' before the .ss, so it is used if the search action is called in the URL.
public static $allowed_actions = array (
'search',
'SearchForm'
);
public function SearchForm() {
$fields = new FieldSet(
new DropDownField('Type', array(
'all' => 'All',
'new' => 'New',
'used' => 'Used'
)),
new TextField('Query','Query')
);
$actions = new FieldSet(
new FormAction('doSearch', 'Search')
);
$form = new Form($this, 'SearchForm', $fields, $actions);
return $form;
}
public function doSearch($data, $form) {
$Query = urlencode($data['Query']);
$Type = $data['Type'];
if ($Type && $Query) Director::redirect($this->Link("search/$Type/$Query")); // Redirect to a search result page i.e. "www.site.com/camcorders/search/used/sony+HD+bargain" you'll probably want to tweak how this part redirects if you're going to have the search form on every page i.e. don't use $this->Link
}
public function SearchResults() {
$Params = $this->getURLParams();
$Action = $Params['Action'];
$Type = $Params['ID'];
$Query = Convert::raw2sql(urldecode($Params['OtherID']));
if (Action == 'search' && $SearchQuery && $Type){
$where ="Title LIKE '%$Query%' OR Description LIKE '%$Query%'";
$sort ='';
$join='';
$error=false;
switch ($Type) {
case 'all':
break;
case 'new':
$where.= " && TypeID = 1";
break;
case 'used':
$where.= " && TypeID = 2";
break;
default:
$error = true;
break;
}
if ($error) return $this->httpError(404);
return DataObject::get('Camcorder', "".$where, $sort, $join);
}
}