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.

General Questions /

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

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

Filter Page data ?


Go to End
Reply


3 Posts   639 Views

Avatar
datswicked

Community Member, 15 Posts

24 October 2012 at 11:07pm

Edited: 27/10/2012 10:46pm

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 {
}

Avatar
datswicked

Community Member, 15 Posts

25 October 2012 at 6:15pm

Edited: 26/10/2012 12:55pm

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

Avatar
datswicked

Community Member, 15 Posts

27 October 2012 at 10:42pm

Edited: 27/10/2012 10:51pm

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.