Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Listing articles using 2 categories and a custom field (from a form)
Is it possible to list / filter articles from a form that has 3 pull-down menus that refer to the 2 categories and a value in one of the custom fields?
For example, a product that has a category for its color, its weight and its cost in a custom field.
Would be great to see something like this in action.
Thanks,
Loz
Offline
Re: Listing articles using 2 categories and a custom field (from a form)
lozmatic wrote:
Is it possible to list / filter articles from a form that has 3 pull-down menus that refer to the 2 categories and a value in one of the custom fields?
It’s hackish but smd_if can do it by using article_custom to iterate over all products and match your user’s criteria.
Build your HTML form with the 3 (easier with 4) dropdowns containing txp:category1, category2 and some lower and upper cost limits. When you submit the HTML form, make it pass these 4 values to the results page as: c1
, c2
, priceL
, priceH
. Then in your form:
<txp:article_custom section="products">
<txp:smd_if field="category1, category2, price, price" operator="eq, eq, ge, le" value="urlvar:c1, urlvar:c2, urlvar:priceL, urlvar:priceH">
<txp:permlink><txp:title /></txp:permlink>
<txp:excerpt />
</txp:smd_if>
</txp:article_custom>
How’s that? Speed is dependent of course on how many products there are in the section.
Alernatively you may try some filtering directly in the article_custom to cut down the amount of work smd_if has to do. Perhaps with tags-in-tags and the adi_gps plugin you can do some pre-filtering this way:
<txp:adi_gps />
<txp:article_custom section="products" category='<txp:variable name="c1" />, <txp:variable name="c2" />'>
<txp:smd_if field="price" operator="ge, le" value="urlvar:priceL, urlvar:priceH">
<txp:permlink><txp:title /></txp:permlink>
<txp:excerpt />
</txp:smd_if>
</txp:article_custom>
Or variations thereof. In both cases you should do some error checking to make sure the variables exist.
Failing that, get smd_query on the job ;-)
Last edited by Bloke (2008-12-30 21:55:36)
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
Re: Listing articles using 2 categories and a custom field (from a form)
Thanks, I’ll give it a go.
Looks like the smd_if plugin is pretty useful.
Cheers,
Loz
Offline
Re: Listing articles using 2 categories and a custom field (from a form)
Hi again,
I’ve got this pretty much working :)
I’m stuck with this, though. For my second dropdown I would like to use the ‘eq’ operator to match a value carried in the URL to category2. But I also want to be able to offer an ‘all products’ option from the pull-down menu so that all articles are displayed.
Offline
Re: Listing articles using 2 categories and a custom field (from a form)
lozmatic wrote:
I would like to use the ‘eq’ operator to match a value carried in the URL to category2. But I also want to be able to offer an ‘all products’ option from the pull-down menu so that all articles are displayed.
Depending how you set your page/forms up, if you made the very first item of you dropdown something like:
<option value="">All products</option>
Then in your form (assuming you’re using TXP 4.0.7+), what about:
<txp:article_custom section="products">
<txp:smd_if field="catgeory2" operator="isempty">
<h2>All products</h2>
// Output all products here
<txp:else />
<txp:smd_if field="category1, category2, price, price" operator="eq, eq, ge, le" value="urlvar:c1, urlvar:c2, urlvar:priceL, urlvar:priceH">
<txp:permlink><txp:title /></txp:permlink>
<txp:excerpt />
</txp:smd_if>
</txp:smd_if>
</txp:article_custom>
Or you could assign the first entry a value, say ALL_PRODUCTS
, and then use the eq
operator to check that value in the first smd_if. Does that work?
Last edited by Bloke (2009-01-07 22:23:22)
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
Re: Listing articles using 2 categories and a custom field (from a form)
Thanks. I got it working as I want.
One odd thing, though, is that if I use <txtp:else />
for a message when there are no results… it gets printed twice. Also, if there is one result the message once. It’s as if there is a phantom positive result at all times.
Anyways, I got it working very well so I’m not going to worry about any phantom results as such.
Offline