Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » Search Form help

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

Page: 1
Go to End
Author Topic: 538 Views
  • tee 1977
    Avatar
    Community Member
    6 Posts

    Search Form help Link to this post

    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

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: Search Form help Link to this post

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

    }

  • tee 1977
    Avatar
    Community Member
    6 Posts

    Re: Search Form help Link to this post

    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

    538 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.