Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-05-10 11:44:35

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

Price search

How do I … search in prices.
What I have is a custom field with a price.
What I want is search in prices from to to, or at least to to (maximum price). As a text input field or select, doesn’t really matter.

For example:
1. I want something between price X and Y. [OK]
2. I want something with maximum price of X. [OK]

Then a search page shows all matches.

Last edited by element (2010-05-10 11:44:55)

Offline

#2 2010-05-10 12:29:54

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,942
Website GitHub

Re: Price search

A few ways to approach it off the top of my head:

Option 1 using the smd_if plugin:

  1. Add the HTML input control(s) for price to your page inside an HTML <form> with the action set to the current page (suggest using min / max as the names for the inputs)
  2. In the page template for that page, put this:
<txp:article_custom section="your_section">
   <txp:smd_if field="?price, ?price" opreator="ge, le" value="urlvar:min, urlvar:max">
 ... display your article fields here
   </txp:smd_if>
</txp:article_custom>

That will simply iterate over every article in the named section and if the Price custom field matches the URL params the article contents will be displayed. A few things to note about this:

  • It’s not very robust: if one or either value is missing from the URL then you’ll have problems so you’ll need to check if they exist first (adi_gps and <txp:if_variable> can help here)
  • It might become slow as the number of articles increases

Option 2 using smd_query:

  1. Add the HTML elements as before
  2. In the page template for the page:
<txp:smd_query query="SELECT * FROM textpattern WHERE Section='your_section' AND custom_N > ?min AND custom_N < ?max" urlfilter="/[^\d]+/" defaults="min:0, max:10000" wraptag="ul" break="li">
   {title} // You could display anything else you wanted here
</txp:smd_query>

(where N is the number of the custom field containing your price field)

The advantage of the smd_query solution is that it scales better, it’s more robust out of the box and you can add pagination to your results. In both cases you’ll probably need some test first to see if the URL contains the values you are interested in, and display your input form controls if it doesn’t. You’ll also need to cater for the situation where only one of the values is submitted or the values are somehow mangled (smd_query helps guard against this with the urlfilter and defaults parameters, but you may need some extra checks).

There are probably other ways of doing it. I’m sure someone else will pipe up with some suggestions.

Last edited by Bloke (2010-05-10 12:31:47)


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 2010-05-11 20:19:06

makss
Plugin Author
From: Ukraine
Registered: 2008-10-21
Posts: 355

Re: Price search

Or use aks_article plugin.

Last edited by makss (2016-03-25 05:56:55)


aks_cron : Cron inside Textpattern | aks_article : extended article_custom tag
aks_cache : cache for TxP | aks_dragdrop : Drag&Drop categories (article, link, image, file)

Offline

Board footer

Powered by FluxBB