Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2009-08-31 22:27:25
- kevinpotts
- Member
- From: Ghost Coast
- Registered: 2004-12-07
- Posts: 370
how to search within a range of arbitrary numeric values?
Quick synopsis: I’m building a website for a real estate company that specializes in large acreage lots. Each listing has its own TXP article, and a custom field is used to store the acres of the property. This could be anywhere from 2 to 2,000.
Is it possible to construct a search mechanism where the user inputs a minimum and maximum acre range, clicks “submit”, the results show any TXP articles where the value of the acres in the custom field falls between the specified range? Here is the current version for reference, built in a somewhat archaic concoction of Access and VB script. Could I use smd_query? Kind of at a loss on this one.
Any help would be appreciated.
Kevin
(graphicpush)
Offline
Re: how to search within a range of arbitrary numeric values?
kevinpotts wrote:
Is it possible to construct a search mechanism where the user inputs a minimum and maximum acre range, clicks “submit”, the results show any TXP articles where the value of the acres in the custom field falls between the specified range?
Yup, a few ways to do it. smd_query is one, as you found (probably the most complete solution). The other way is the poor man’s query as I call it. The thread there goes into it in more detail but the essence is to use smd_if like this to read the values from your submitted HTML form and only display matching entries from a giant article_custom:
<txp:article_custom section="properties" limit="9999">
<txp:smd_if field="custom_3, custom_3" operator="gt, lt" value="urlvar:min, urlvar:max">
<txp:permlink><txp:title /></txp:permlink>
<txp:excerpt />
</txp:smd_if>
</txp:article_custom>
The success of this technique is dependent on the number of articles in your database. Note also that paging and limiting don’t work. There are probably other ways to approach it (adi_gps might help).
EDIT: you would probably want to use smd_if’s filtering capability to make sure that the input values from the url were all numeric. Definitely worth doing from a safety aspect.
Last edited by Bloke (2009-08-31 22:48:51)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
#3 2009-09-02 01:31:38
- kevinpotts
- Member
- From: Ghost Coast
- Registered: 2004-12-07
- Posts: 370
Re: how to search within a range of arbitrary numeric values?
This is very helpful, and I think I understand the logic. But how would the HTML form look? What would the action be, or the values for the two fields?
Kevin
(graphicpush)
Offline
Re: how to search within a range of arbitrary numeric values?
Dunno, maybe summat like this (untested):
<txp:smd_if field="urlvar:min, urlvar:max" operator="isused, isused">
<!-- Both defined, so grab the results -->
<txp:article_custom section="properties" limit="9999" label="Results">
<txp:smd_if field="custom_3, custom_3" operator="gt, lt" value="urlvar:min, urlvar:max">
<txp:permlink><txp:title /></txp:permlink>
<txp:excerpt />
</txp:smd_if>
</txp:article_custom>
<txp:else />
<!-- not defined so show the form. It should submit to the same page,
though might require an action attribute to validate -->
<form name="myform" method="get">
<br />Min: <input type="text" name="min" />
<br />Max: <input type="text" name="max" />
<input type="submit" value="Search" />
</form>
</txp:smd_if>
Last edited by Bloke (2009-09-02 10:33:52)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
#5 2009-09-07 02:41:23
- kevinpotts
- Member
- From: Ghost Coast
- Registered: 2004-12-07
- Posts: 370
Re: how to search within a range of arbitrary numeric values?
Stef — I have no idea how this stuff is working, but with a few tweaks, this worked wonderfully. You are an absolute genius. Really. Thank you. Here it is in action.
Two more questions for posterity:
1.) What is the operator for “greater than or equal to” and “less than or equal to” instead of just “greater than” and “less than”? This way the results would show any property that matches the input value.
2.) How can I output the search results? If I wanted to create a header that said “Search Results for Properties Between XX and XX Acres”, what tag would use to echo the vlaues input into the search field?
Thanks for your help to this point.
Kevin
(graphicpush)
Offline
Re: how to search within a range of arbitrary numeric values?
kevinpotts wrote:
with a few tweaks, this worked wonderfully. You are an absolute genius.
Aww shucks. Glad it’s working for ya.
the operator for “greater than or equal to” and “less than or equal to”
ge
and le
respectively. There are a tonne more listed in the plugin help that might be of assistance.
If I wanted to create a header that said “Search Results for Properties Between XX and XX Acres”
You can use the replacement tag syntax. Again, the docs explain more but in a medium-sized conker husk, you are given one “replacement variable” for every field you have tested. Thus in your form/container inside smd_if you can put {smd_if_min}
and {smd_if_max}
wherever you want the values of min
and max
to be displayed.
Last edited by Bloke (2009-09-07 08:17:09)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
#7 2009-09-07 18:45:51
- kevinpotts
- Member
- From: Ghost Coast
- Registered: 2004-12-07
- Posts: 370
Re: how to search within a range of arbitrary numeric values?
ge and le respectively
Thanks. I was looking in the documentation on your site and missed that list. I should have checked the plugin help, knowing how thorough you are in your other plugins.
You can use the replacement tag syntax. Again, the docs explain more but in a medium-sized conker husk, you are given one “replacement variable” for every field you have tested. Thus in your form/container inside smd_if you can put {smd_if_min} and {smd_if_max} wherever you want the values of min and max to be displayed.
Ah, again I missed that.
Everything works perfectly and I owe you big time. Thanks for the help. I hope others find this bit helpful in the future.
I really have to dig into smd_query and smd_if. It looks like there is a new universe of power there, much like smd_gallery. Great work.
Kevin
(graphicpush)
Offline