The only way I knew of filtering MODx ressources, depending on multiple filters set by users, was the tvExplorer Snippet. However, recently I stumbled upon a russian MODx blog which showed another approach ( parts of it from the comments section of that blog post ) which will work for basic filtering with Ditto - see screenshot below:

ditto_filters01

tvExplorer still is the way to go for more extended filter options btw.

In order to setup a search mask as shown above, we'll create a ressource folder "Apartments" which will hold all our apartments ( and one ressource for each apartment ).

ditto_filters02

1. create TVs for different filter options:

Template Variable Name: tvcity
Input Type: DropDown List Menu
Input Option Values: Berlin==Berlin||Frankfurt==Frankfurt||Hamburg==Hamburg||Kiel==Kiel etc.

Template Variable Name: tvprice
Input Type: DropDown List Menu
Input Option Values: 100==100||200==200||400==400||600==600 etc.

Template Variable Name: tvroom
Input Type: DropDown List Menu
Input Option Values: one==one||two==two||three==three||four==four etc.

2. place the following code in your "Apartments" folder ( change "id=11" to the id of your folder ):

<form action="index.php?id=11" method="post">
City:
<select name="city">
<option>all</option>
<option value="Berlin">Berlin</option>
<option value="Frankfurt">Frankfurt</option>
<option value="Hamburg">Hamburg</option>
<option value="Kiel">Kiel</option>
</select>
Price:
<select name="price">
<option>all</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="400">400</option>
<option value="600">600</option>
</select>
Rooms:
<select name="room">
<option>all</option>
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
</select>
<input name="Submit" type="submit" value="Search" />
</form>
[!dittofilter!]

3. create a new Snippet "dittofilter":

<?php
global $modx;
$filter = "";
if (!empty($_POST['city'])) $filter .= (empty($filter)?"":"|")."tvcity,".mysql_escape_string($_POST['city']).",1";
if (!empty($_POST['price'])) $filter .= (empty($filter)?"":"|")."tvprice,".mysql_escape_string($_POST['price']) .",1";
if (!empty($_POST['room'])) $filter .= (empty($filter)?"":"|")."tvroom,".mysql_escape_string($_POST['room']) .",1";
return $modx->runSnippet('Ditto', array('parents'=>'11', 'tpl'=>'apartments', 'display'=>'10', 'sortBy'=>'pub_date', 'dateSource'=>'pub_date', 'dateFormat'=>'%d.%m.%Y', 'paginate'=>'1', 'filter'=>$filter, 'noResults'=>'no results'));
?>

this example uses the Ditto parameter 'tpl'=>'apartments', so either create a Ditto template Chunk "apartments" or change the name in the array to your template Chunk. All Ditto parameters are set in that array.
/////// *important*
If you use other TV names and select names as in my example, change them accordingly in the Snippet code.

Now you should be ready to go :) set the different TV options for your apartment ressources and you'll be able to search them according to the form input. e.g. "Berlin" "200" "two" will schow all apartments in your folder with the TVs set to Berlin, 200 & two. The initial Ditto call will list all apartments.

ditto_filters03

/////// *tip*
if you don't want to loose the selected filter options after submitting the form, you could use a Snippet in order to return the option list. e.g. the Snippet for citys could look like this - Snippet "selectedcity":

<?php
$output="";
$values = array('Berlin','Frankfurt','Hamburg','Kiel');

$output='<select name="city">';
$output.='<option value="">all</option>';

for($x = 0; $x < count($values); $x++)
{
if($values[$x] == $_POST['city'])
{
$selected = 'selected';
}else{
$selected = '';
}
$output.='<option value="'.$values[$x].'"'.$selected.'>'.$values[$x].'</option>';
}
$output.='</select>';

return $output;
?>

/////// *important* please make sure that the POST variable is made secure / i'll update the snippet

...now place the form on your folder ressource as follows:

<form action="index.php?id=11" method="post">
City:
[!selectedcity!]
Price:
[!selectedprice!]
Rooms:
[!selectedroom!]
<input name="Submit" type="submit" value="Search" />
</form>
[!dittofilter!]

If you got any suggestions, improvements etc. leave a comment, pm me on the forum (sharkbait).

/////// *added*
in order to keep pagination, the search parameters need to be passed via GET. Search form:

<form action="[~[*id*]~]" method="get">
City:
[!selectedcity!]
Price:
[!selectedprice!]
Rooms:
[!selectedroom!]
<input name="Submit" type="submit" value="Search" />
</form>
[!dittofilter!]

and change all occurances of POST ( Snippet "dittofilter", "selectedcity" etc. ) to GET.
this will work, and i hope it's legit ... maybe you could reassure with a forum post :)

Showing comments 1 to 16 of 51 | Next | Last
Maggie
Gravatar Image
Reply #51 on : Wed January 11, 2012, 05:30:07
Thanks for sarhing. What a pleasure to read!
Alexander
Gravatar Image
Reply #50 on : Sun December 18, 2011, 15:15:11
Thanks, this thing helped me! But now I need sort the stuff through the site after filter. How can I do it without filter loss?
webpaul
Gravatar Image
Reply #49 on : Wed November 16, 2011, 13:06:15
Hi again.
The script is working great. The only problem I've found so far and can't resolve is pagination. I'm using the script:
"<p>Showing <strong></strong> - <strong></strong> of <strong></strong> Articles | Page <strong></strong> of <strong></strong> </p> <div id="ditto_pages"> </div> "
Without filtering it's working fine. Don't know where the right place to put it with dittofilter Snippet.
Buddy
Gravatar Image
Reply #48 on : Thu October 13, 2011, 06:27:13
@Julie (reply #26)
Thanks for supplying that code for the check boxes. I tried running it on my site and I get the following modx parsing error. Any ideas?

« MODx Parse Error »
MODx encountered the following error while attempting to parse the requested resource:
« PHP Parse Error »

PHP error debug
Error: Invalid argument supplied for foreach()
Error type/ Nr.: Warning - 2
File: /var/www/clients/client3/web119/web/manager/includes/document.parser.class.inc.php(814) : eval()'d code
Line: 7

Parser timing
MySQL: 0.0013 s (5 Requests)
PHP: 0.0696 s
Total: 0.0709 s
4r
Gravatar Image
Reply #47 on : Wed July 06, 2011, 12:38:33
I have it working finally. (used GET)
To get all results it needs value="".

<option value="">all</option>

The problem is, I need to add manually new citys to the dropdown TV for the form and TV, and if u use there too.
4r
Gravatar Image
Reply #46 on : Thu June 09, 2011, 12:54:46
I wonder, is it possible to show the city's which are written into the city TV?

This below is handwritten, i need to see the city's which are entered into the city TV, or do i need to use the TVexplorer for this?

Input Option Values: Berlin==Berlin||Frankfurt==Frankfurt||Hamburg==Hamburg||Kiel==Kiel etc.
Lissie
Gravatar Image
Reply #45 on : Mon April 18, 2011, 22:38:44
Having this problem: after submitting form, I see the URL parameters after the friendly URL, but only for a second, then the initial state of the URL loads.
Tomasz
Gravatar Image
Reply #44 on : Tue April 12, 2011, 15:20:51
Hi!
Is it possible to retrieve 'cities' from TV definition? Entering the same data in several places will result in hell in case of need to correct some positions.
admin
Gravatar Image
Reply #43 on : Wed March 16, 2011, 18:46:59
no, i assume you got the URL 'hardcoded' e.g.
action="index.php?id=11"
use action="[ ~ [ * id * ] ~ ]" instead
(delete the spaces)

*edited*
Last Edit: March 16, 2011, 19:10:19 by admin  
webpaul
Gravatar Image
Reply #42 on : Wed March 16, 2011, 18:37:02
Even larger problem. After turning "Friendly URL" on page before search is shown with friendly URL and after with ?id-63. Is it common problem?
admin
Gravatar Image
Reply #41 on : Wed March 16, 2011, 15:22:50
hey webpaul,

to keep the pagination, you'll need to use the 'Get' version ..and the 'Get' version needs FURLs enabled :) it's because '?' is already used in the URL (e.g. index.php?id=). if i get some time, i'll try to update the code
webpaul
Gravatar Image
Reply #40 on : Wed March 16, 2011, 14:49:58
I got it working this time by changing GET to POST. You are the best. Thank you. But now different problem. My paging stopped working. Oh man!!!
webpaul
Gravatar Image
Reply #39 on : Wed March 16, 2011, 13:51:12
Do I have to? I usually don't use it
admin
Gravatar Image
Reply #38 on : Wed March 16, 2011, 12:35:47
Hi webpaul,

'friendly URLs' are enabled?
webpaul
Gravatar Image
Reply #37 on : Wed March 16, 2011, 12:17:48
Hi to everyone.
Great staff. Can't get it work. Do exactly as you say:
<form action="" method="get">
Length:

Year:

Price:

<input name="Submit" type="submit" value="Search" />
</form>
<p>
</p>

<?php
global $modx;
$filter = "";
if (!empty($_GET['boatsize'])) $filter .= (empty($filter)?"":"|")."tvboatsize,".mysql_escape_string($_GET['boatsize']).",1";
if (!empty($_GET['year'])) $filter .= (empty($filter)?"":"|")."tvyear,".mysql_escape_string($_GET['year']) .",1";
if (!empty($_GET['price'])) $filter .= (empty($filter)?"":"|")."tvprice,".mysql_escape_string($_GET['price']) .",1";
return $modx->runSnippet('Ditto', array('parents'=>'63', 'tpl'=>'gallery', 'display'=>'3','paginate'=>'1', 'filter'=>$filter, 'noResults'=>'no results'));
?>
After "Search", throws me to the main page of my project. Any ideas?
admin
Gravatar Image
Reply #36 on : Thu February 03, 2011, 10:13:38
Привет dsniff,

включить дружественные URL-адресов :)

j
  • Required fields are marked with *.

If you have trouble reading the code, click on the code itself to generate a new random code.