Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-04-03 00:02:33

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

How can I limit the number of returned Articles when limit= won't do?

Hi TXPers,

Sorry I have had to return here to draw from the well of knowledge and assistance, again…

{:-(

I have this in my Page template:

<txp:article_custom limit="50" form="events_future" section="events" sort="custom_6 asc" />

It’s great, it returns articles (titles and links) for matching Articles in my Events section.

I actually want it to return only 5 (not 50) and if I set limit="5" then if the first five records found pass a test carried out in the form events_future then all is fine, I get 5 Articles back. The problem is that the form does a math test to see if each article is a candidate for return, and with my limit="5" it seems it will find 5, test 5 and return those that passed the test. So often I will not get 5 articles returned.

I think I must be missing something basic here, but fruitless after long attempts, I return here, intellectual-cap-in-hand to ask the clever people if they can throw me a crumb or two of wisdom.

Thank you so much in advance if you’re able to shed some light on this for me. Cheers, -Alan


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#2 2010-04-03 00:21:27

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: How can I limit the number of returned Articles when limit= won't do?

What’s the test?

Broadly, possible solutions are to include the test in the query that fetches the article in the first place, or to use your method (limit="50") but then count the articles that pass the test. The first solution would usually involve an article_custom plugin (or smd_query), the second might use adi_calc.


Code is topiary

Offline

#3 2010-04-03 02:20:51

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: How can I limit the number of returned Articles when limit= won't do?

The form does this:

<txp:variable name="kate" value='<txp:custom_field name="Event date" />' />
<txp:variable name="julie" value='<txp:rah_replace from="/" to="-"><txp:variable name="kate" /></txp:rah_replace>' />
<txp:variable name="numEvent" value='<txp:smd_cal_now format="%s" now=''<txp:variable name="julie" />'' />' />
<txp:variable name="numNow" value='<txp:smd_cal_now offset="-1 day" format="%s" />' />

…and then the test is this:

<txp:smd_if field='<txp:variable name="numEvent" />' operator="gt" value='<txp:variable name="numNow" />'>

So I suppose it’s too complicated to do at the time the article is fetched?

I’ll look at smd_query and/or adi_calc — I’m typing brave there, I’m not sure I’ll know how to apply them ;) but I’ll go look and try! Thanks lots for the suggestions!


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#4 2010-04-03 10:06:09

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

Re: How can I limit the number of returned Articles when limit= won't do?

alanfluff wrote:

So I suppose it’s too complicated to do at the time the article is fetched?

Given the amount of stuff you’re doing in those variable tags I’d say it’d be more efficient to count like jsoo suggests:

<txp:variable name="article_count" value="0" />
<txp:article_custom limit="some_giant_value"... blah blah>
   <txp:variable stuff here />
...
   <txp:smd_if field='<txp:variable name="numEvent" />, article_count' operator="gt, lt" value='<txp:variable name="numNow" />, 5'>
      // output your article bits n bobs here
      <txp:adi_calc name="article_count" add="1" />
   </txp:smd_if>
</txp:article_custom>

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 2010-04-03 11:03:59

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: How can I limit the number of returned Articles when limit= won't do?

Thanks, yes I tried that and it was successful, almost…

The articles were processed by the loop and when five (what I wanted) were found, returned to the original calling article_custom and hence the page. Perfect. The problem was that the looped-through articles where the loop was looking for five before it ended, looped in an order that didn’t necessarily find a contiguous block and so sometimes my 5 was actually #‘s 1, 2, 3, 4, 6.

Explaining that’s got me thinking, why was this order wrong… I’ll go back and check my code. Thanks again!


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#6 2010-04-03 13:21:56

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

Re: How can I limit the number of returned Articles when limit= won't do?

alanfluff wrote:

my 5 was actually #‘s 1, 2, 3, 4, 6.

Ah, sorry, perhaps I misread your original problem. What the version I posted above does is check each article to see if it matches what you wanted in your custom fields/variables AND if the count is less than 5. So if the 5th article doesn’t match, it’ll be excluded regardless if it’s the 5th article to be looked at.

If instead, you want to simply find the first occurrence of an article that matches your given txp:variable criteria and then return that article and the next 4 you’ll have to approach it differently.

If that’s not what you want and it’s close enough already except for the sorting, check the custom_6 content for the ‘out of sequence’ articles. You might find things like 3/5/2010 appears ‘later’ than 04/5/2010 (or something like that) because of the leading zero. Sometimes, if your field contains other alphanumeric characters then greater than/less than can give less-than-obvious results.


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 2010-04-03 13:29:07

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: How can I limit the number of returned Articles when limit= won't do?

No, my bad for not explaining more fully — always a conundrum, don’t want to bore ;) but want to be complete/clear.

Long story short, I have learnt TONS and thanks for all comments, but I have just done a db and file back-up (in case) and am now going back to basics and throwing away all logic and starting from scratch in the hope I can greatly simplify. I think I’ll be OK now, especially with this learning curve under my mixed-metaphor belt.

No dis to plugins, but it is lovely to be using fewer and instead trying to make more and subtler use of core tags.


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#8 2010-04-03 13:37:49

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

Re: How can I limit the number of returned Articles when limit= won't do?

alanfluff wrote:

now going back to basics and throwing away all logic and starting from scratch

Hehehe, it’s painful but sometimes that’s the only way to approach something once you’ve realised you’ve over-engineered it. Case in point: 90% of my plugins ;-)

If I’d waited until now to write them they’d be half the size and half the complexity because of stuff I’ve learnt over the past few years. smd_calendar is the latest casualty and it’s causing more than its fair share of headaches to get it optimized and working reliably. I need to divert more brain power and time to it than I have available right now :-(

No dis to plugins, but it is lovely to be using fewer and instead trying to make more and subtler use of core tags.

Yeah it’s nice isn’t it? I’m hoping that when the next version of TXP comes out, people can gradually stop relying so heavily on smd_gallery / upm_image / soo_image / wet_for_each_image / etc for all but the most complex gallery solutions and use the new, leaner core image tags instead.

Last edited by Bloke (2010-04-03 13:44:48)


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

#9 2010-04-03 13:43:11

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: How can I limit the number of returned Articles when limit= won't do?

Getting a headache just trying to imagine how hard it must be to dev/support something as sophisticated as smd_calendar. Much respect for getting such an amazing plugin out at all — I loaded it and almost did the literal jaw drop when I saw that lovely calendar rendered! I’m sure I’ll be using it, but hopefully as a proper calendar and not just so I can use one of it’s tags ;)

V.best of luck with dev of the plugin(s) and all you do (and thanks for making me literally LOL with some of the rnd photo-caption combos on your site!


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

Board footer

Powered by FluxBB