Jump to:

23479 Posts in 18951 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Filter Page data ?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 428 Views
  • datswicked
    Avatar
    Community Member
    15 Posts

    Filter Page data ? Link to this post

    Hi All, i hope someone can point me in the right direction, bit of a nOOb at this.

    i have setup a little dealer area for my site and am trying to filter the results via 2 data fields but am not sure if i have even set it up right.
    can any please point me in the right direction..
    i want to be able to filter either or both `state` and `SearchCategory` i have set up a form hand coded that submits to the dealerHolder template but am not sure where to go next?

    //// dealerHolder.php

    <?php
    class DealersHolder extends Page {
    static $allowed_children = array( 'Dealers' );
    }
    class DealersHolder_Controller extends Page_Controller {   
    }

    //// dealers.php

    <?php
    class Dealers extends Page {
       static $allowed_children = array( 'DealerCarsHolder', 'DealerContact' );
       static $default_child = 'DealerCarsHolder';
       static $searchable_fields = array(
          'State' => 'ExactMatchFilter',
          'SearchCategory' => 'ExactMatchFilter'
       );
       public static $db = array(
    'Street' => 'Varchar(255)',
    'Suburb' => 'Varchar(150)',
       'State' => "Varchar(40)",
    'Postcode' => 'Varchar(100)',
    'Phone' => 'Varchar(100)',
    'Mobile' => 'Varchar(100)',
    'Fax' => 'Varchar(100)',
    'Url' => 'Varchar(255)',
    'SearchCategory' => 'Varchar(255)'
    );

       function getCMSFields() {
    $fields = parent::getCMSFields();   
          $fields->addFieldToTab('Root.Content.Main', new TextField('Street', _t('Partners.Street', 'Street Address'), ''),'Content');
          $fields->addFieldToTab('Root.Content.Main', new TextField('Suburb', _t('Partners.Suburb', 'Suburb'), ''),'Content');   
          $fields->addFieldToTab('Root.Content.Main', new DropdownField( 'State', 'State', array(
              'VIC'    => 'Victoria',
              'NSW'    => 'New South Wales',
              'ACT'   => 'Australian Capital Territory',
              'QLD'    => 'Queensland',
              'NT'    => 'Northern Territory',
              'SA'     => 'South Australia',
              'WA'    => 'Western Australia',
              'TAS'    => 'Tasmania',
              )), 'Content');
           $fields->addFieldToTab('Root.Content.Main', new NumericField('Postcode', _t('Partners.Postcode', 'Postcode'), '', 4),'Content');
           $fields->addFieldToTab('Root.Content.Main', new TextField('Phone', _t('Partners.Phone', 'Phone ( please format like this (00) 0000-0000 )'), ''),'Content');
           $fields->addFieldToTab('Root.Content.Main', new TextField('Mobile', _t('Partners.Mobile', 'Mobile ( please format like this 0000-000-000 )'), ''),'Content');
           $fields->addFieldToTab('Root.Content.Main', new TextField('Fax', _t('Partners.Fax', 'Fax ( please format like this (00) 0000-0000 )'), ''),'Content');
           $fields->addFieldToTab('Root.Content.Main', new TextField('Url', _t('Partners.Url', 'Url ( please include http://www. )'), ''),'Content');
          $fields->addFieldToTab('Root.Content.Main', new DropdownField( 'SearchCategory', 'Searchable Category', array(
              'dealerType1'    => 'dealer Type 1',
              'dealerType2' => 'dealer Type 2',
              'dealerType3'   => 'dealer Type 3',
              'dealerType4'    => 'dealer Type 4',
              'dealerType5'    => 'dealer Type 5'
              )), 'Content');
    return $fields;
    }
    }
       
    class Partners_Controller extends Page_Controller {
    }

  • datswicked
    Avatar
    Community Member
    15 Posts

    Re: Filter Page data ? Link to this post

    Hi Everyone,

    not sure if my question was clear or not so i will try again.

    I have to have a listing of dealers and i want to be able to filter that data from a from on a separate page. they would be able to choose state and type to filter on.

    I hope some one has an answer for this. it seems like it should be an easy part of the cms to do but i cant really find documentation on this? although i might not be looking for the right thing so any info would be great.

    I hope you can help
    Thanks

  • datswicked
    Avatar
    Community Member
    15 Posts

    Re: Filter Page data ? Link to this post

    Have found the solution.. DataObject::get($obj, $filter, $sort, $join, $limit);

    Just in case someone finds this with the same problem here is my solution.

    From my form i posted to my dealerHolder.php page
    <form action="{$BaseHref}dealers/" method="post">
    sending `state` and `SearchCategory` variables.

    on
    //// dealerHolder.php

    <?php
    class DealersHolder extends Page {
    static $allowed_children = array( 'Dealers' );
    }
    class DealersHolder_Controller extends Page_Controller {
       // create the function to process the form request
       function getDealers(){
          $filter = '';
          
          // check if the state is passed or not
          if(isset($_POST['state'])){
             // escape for security
             $state = mysql_real_escape_string($_POST['state']);
             // add to filter
             $filter .= 'State = \''.$state.'\'';
          }
          // check if the SearchCategory is passed or not
          if(isset($_POST['SearchCategory'])){
             // escape for security
             $SearchCategory = mysql_real_escape_string($_POST['SearchCategory']);
             if($filter!='')
                $filter .= ' AND ';
             // Add to filter   
             $filter .= 'SearchCategory = \''.$SearchCategory.'\'';
          
          }
          $records = DataObject::get('Partners', $filter);
          return $records;
          
       }
       
    }

    Then i looped through the request on the template
    // dealerHolder.ss

    <% if getDealers %>      
       <% control getDealers %>
          <h2>$Title</h2>
          $Content
       <% end_control %>
    <% else %>
       <p>Sorry, your search query did not return any results.<br> Please Try again.</p>
    <% end_if %>

    i did not edit the dealers.php any further.

    428 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.