Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Search Form help


Go to End


3 Posts   1118 Views

Avatar
tee 1977

Community Member, 6 Posts

9 November 2011 at 11:07pm

i've recently started a website for a company that sells new and used camcorders, they are not selling online but one of the functions of the site is to have a more advanced search form at the top of each page. basically before the search box text field there would either be a drop down select field or radio buttons where the customer can choose to search for new or used products, then they would be able to search via the input field. i have set up 2 sections on the website, one for new and another for used and have a dataobject list of products in each. Im really stuck as to how to implement this. can anyone help me with this?

Many thanks

Tamara

Avatar
JonoM

Community Member, 130 Posts

10 November 2011 at 11:57am

Edited: 10/11/2011 12:02pm

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

}

Avatar
tee 1977

Community Member, 6 Posts

10 November 2011 at 12:39pm

Hi Jono

This looks exactly what i'm after, i will give it a go when im back from hols in a week but thanks so much for your reply.

Cheers

Tamara