Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#37 2014-05-13 15:25:50

etc
Developer
Registered: 2010-11-11
Posts: 4,665
Website GitHub

Re: etc_search: when the default search is not enough

Hello,

currently, it works like this:

  • create (in admin etc_search tab) an article-type search form with query
{custom_1::/^(\d+)\.\.(\d+)$/::{*} BETWEEN $1 AND $2}
  • assuming the above form has id=1, create (in your page form) the following search input form:
<txp:etc_search id="1" label="Custom_1 between" format="{min}..{max}">
<input name="q" type="hidden" value="1" /><!-- to trigger if_search -->
<select name="min">
	<option value="0" selected="selected">Choose min value ...</option>
	<option value="2000">2000</option>
	<option value="4000">4000</option>
	<option value="6000">6000</option>
	<option value="8000">8000</option>
	<option value="10000">10000</option>
</select>
<select name="max">
	<option value="10000" selected="selected">Choose max value ...</option>
	<option value="2000">2000</option>
	<option value="4000">4000</option>
	<option value="6000">6000</option>
	<option value="8000">8000</option>
	<option value="10000">10000</option>
</select>
<input type="submit" />
</txp:etc_search>
  • replace <txp:article /> with <txp:etc_search_results /> in the if_search part of your page form.

Quickly tested, it should work, but don’t hesitate about feedback/tuning.

Offline

#38 2014-05-13 15:53:31

element
Member
Registered: 2009-11-18
Posts: 98

Re: etc_search: when the default search is not enough

Thanks for the help. It looks like it works :-)
One more question (hopefully), is there a way to sort the search results according to custom_1?

Offline

#39 2014-05-13 16:01:19

etc
Developer
Registered: 2010-11-11
Posts: 4,665
Website GitHub

Re: etc_search: when the default search is not enough

element wrote #280779:

One more question (hopefully), is there a way to sort the search results according to custom_1?

Yes, by adding ORDER BY custom_1 (ASC or DESC) to query:

{custom_1::/^(\d+)\.\.(\d+)$/::{*} BETWEEN $1 AND $2} ORDER BY custom_1

You can also add additional WHERE conditions if necessary:

{custom_1::/^(\d+)\.\.(\d+)$/::{*} BETWEEN $1 AND $2} AND Section='products' ORDER BY custom_1

Last edited by etc (2014-05-13 16:02:30)

Offline

#40 2014-05-13 16:18:26

element
Member
Registered: 2009-11-18
Posts: 98

Re: etc_search: when the default search is not enough

Right, didn’t think about it. Thanks.

I had to add +0 to sort them properly.

{custom_1::/^(\d+)\.\.(\d+)$/::{*} BETWEEN $1 AND $2} ORDER BY custom_1+0

Offline

#41 2014-05-13 16:23:39

etc
Developer
Registered: 2010-11-11
Posts: 4,665
Website GitHub

Re: etc_search: when the default search is not enough

element wrote #280781:

I had to add +0 to sort them properly.

Good catch, glad it works.

Offline

#42 2014-05-13 16:51:36

element
Member
Registered: 2009-11-18
Posts: 98

Re: etc_search: when the default search is not enough

It works on a PHP 5.3 server, not on a PHP 5.2 server.

Damn, I tried the solution in this post. I replaced the line 392, added the function and the plugin installs but the search on the website doesn’t work.

I get this when I use the search: Fatal error: Call to undefined function safe_escape() in /home/xxxx/public_html/textpattern/lib/txplib_misc.php(653) : eval()’d code on line 421

Offline

#43 2014-05-13 17:20:18

etc
Developer
Registered: 2010-11-11
Posts: 4,665
Website GitHub

Re: etc_search: when the default search is not enough

Ah yes, sorry, safe_escape() has been introduced in 4.5.0. You can replace the line

$params['q'] = safe_escape($params['q']);

with

global $DB;
$params['q'] = mysql_real_escape_string($params['q'], $DB->link);

in the plugin code.

Edit: though this function will be depreciated in future PHP versions.

Last edited by etc (2014-05-13 17:26:55)

Offline

#44 2014-05-13 17:39:12

element
Member
Registered: 2009-11-18
Posts: 98

Re: etc_search: when the default search is not enough

Thanks, etc. You’re awesome. It works!

Offline

#45 2014-05-13 18:30:54

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: etc_search: when the default search is not enough

etc wrote #280785:

safe_escape()

You may want to use doSlash() instead — it’s backwards compatible too. We don’t advice calling safe_escape() directly due to b/c, and because the current wrapper will eventually be just a wrapper for wrapper itself.

Last edited by Gocom (2014-05-13 18:33:53)

Offline

#46 2014-05-14 08:34:57

etc
Developer
Registered: 2010-11-11
Posts: 4,665
Website GitHub

Re: etc_search: when the default search is not enough

Gocom wrote #280789:

You may want to use doSlash() instead — it’s backwards compatible too.

I will, thanks. Actually, I’ve always thought of these doSomething() functions as mere shortcuts for doArray(), which is unnecessary in this case.

Offline

#47 2014-08-07 14:03:26

element
Member
Registered: 2009-11-18
Posts: 98

Re: etc_search: when the default search is not enough

I’m using this code to search between 2 integer values (min and max).

<txp:etc_search id="1" label="Custom_1 between" format="{min}..{max}">
<input name="q" type="hidden" value="1" /><!-- to trigger if_search -->
...
</txp:etc_search>

<txp:search_term /> always shows the value 1. How do I get the 2 integers to show up on my page as search terms?

Last edited by element (2014-08-07 14:07:11)

Offline

#48 2014-08-07 16:59:41

etc
Developer
Registered: 2010-11-11
Posts: 4,665
Website GitHub

Re: etc_search: when the default search is not enough

element wrote #282714:

<txp:search_term /> always shows the value 1. How do I get the 2 integers to show up on my page as search terms?

Yes, it merely outputs the value of q input, which is 1 in your case. There is currently no <txp:etc_search_term />, but I will think about it. For the moment, the simplest option is to replace <txp:search_term /> with

<txp:php>
	echo intval(gps('min')).' and '.intval(gps('max'));
</txp:php>

Offline

Board footer

Powered by FluxBB