Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#205 2014-01-15 09:30:59

etc
Developer
Registered: 2010-11-11
Posts: 5,161
Website GitHub

Re: etc_query: all things Textpattern

Glad you like it, Stacey, thanks.

photonomad wrote #278144:

One thing that I’m stuck on at the moment is whether it is possible to use {$?(if|then|else} or another method to test if the start time minutes = 00. If {$?(if|then|else} is only used to check for emptiness, should I just define a <txp:variable name=“startminutes” value=”{$date(i,{start?})}” /> and then use <txp:if_variable name=“startminutes” value=“00”> to test?

You can chain it with another etc_query function: =

{$date(i,{start?}).=00.?(then,else)}

etc_query is so powerful and you make it seem so easy! I wish I could fully understand all of the shortcuts and possibilities.

Thank you again, but, frankly speaking, if my code contained many {$...} or <txp:tag> blocks, I would consider switching to <txp:php>. Here is why: User Defined Functions in PHP are slow, and every {$...} pattern (as well as <txp:tag>) calls few UDFs. That’s ok on small datasets, but when your array contains ~300 rows, the difference starts to be noticeable (I’ve measured ~0.05s). In fact, these {$...} were never intended as replacement for <txp:php>. Bu I hope things can be sped up, so stay with etc_query.

Offline

#206 2014-02-10 13:13:06

johnno
Member
Registered: 2007-06-14
Posts: 30

Re: etc_query: all things Textpattern

Is it possible to inject a txp variable into the url attribute?
Thanks for the plugin btw!

Offline

#207 2014-02-10 13:39:08

etc
Developer
Registered: 2010-11-11
Posts: 5,161
Website GitHub

Re: etc_query: all things Textpattern

johnno wrote #278750:

Is it possible to inject a txp variable into the url attribute?

Certainly, as with any txp tag:

<txp:etc_query url='http://<txp:variable name="url" />/something/else' />

Offline

#208 2014-02-10 14:03:44

etc
Developer
Registered: 2010-11-11
Posts: 5,161
Website GitHub

Re: etc_query: all things Textpattern

etc wrote #278148:

if my code contained many {$...} or <txp:tag> blocks, I would consider switching to <txp:php>.

BTW, I’ve done some testing here, that might interest every txp tags aficionado.

Offline

#209 2014-02-10 14:25:25

johnno
Member
Registered: 2007-06-14
Posts: 30

Re: etc_query: all things Textpattern

etc wrote #278752:

Certainly, as with any txp tag:

<txp:etc_query url='http://<txp:variable name="url" />/something/else' />...

I assumed it should work – and it does; thanks for confirming so quickly.

It’s my ill-disciplined syntax which was causing the problem :(

<txp:etc_query url="http://<txp:variable name='url' />/something/else" />

Offline

#210 2014-03-22 10:33:10

nardo
Member
From: tuvalahiti
Registered: 2004-04-22
Posts: 743

Re: etc_query: all things Textpattern

Oleg, this plugin is fantastic. Thank you again.

A question about extracting a random set of nodes. I’m getting a response from the Flickr API describing 200 photos. I’d like to choose 12 of the nodes at random to display, but I want to avoid the same node being picked twice.

Can it be done in etc_query? Or do I need to first come up with my values, and then populate the query?

query="//photos/photo[33] | //photos/photo[143] | -etc- "

Offline

#211 2014-03-22 12:08:39

etc
Developer
Registered: 2010-11-11
Posts: 5,161
Website GitHub

Re: etc_query: all things Textpattern

Hi, nardo, thank you.

There is no random array generator in XPath, so you’ll need to populate the query yourself. If the number of photos isn’t fixed (200), you can try the following (with the latest version):

<!-- import the feed and count the photos -->
<txp:etc_query url="flickr_url" ...
	query="count(//photos/photo)"
	name="count" save="photos" />

<!-- generate a random query and extract photos -->
<txp:etc_query data="photos" markup="data"
	query='//photos/photo[position()=<txp:etc_query data="{?count||range(1|$).array_rand($|12).@shuffle.implode(+1 or position()=|$)}" />+1]'>
	...
</txp:etc_query>

I can test it if fails (and if you send me the url).

Edit: actually, the shuffle above is meaningless, it should be

<!-- generate a random query and extract photos -->
<txp:etc_query data="photos" markup="data" parse="before">
	<txp:etc_query data="[{?count||range(1|$).array_rand($|12).@shuffle.implode(,|$)}]" markup="json">
	{{//photos/photo[position()={?}+1]/@id?}}
	</txp:etc_query>
</txp:etc_query>

Last edited by etc (2014-03-23 09:58:08)

Offline

#212 2014-03-22 21:57:14

nardo
Member
From: tuvalahiti
Registered: 2004-04-22
Posts: 743

Re: etc_query: all things Textpattern

etc wrote #279892:

I can test it if fails (and if you send me the url).

Works perfectly, thank you!

Offline

#213 2014-04-16 02:19:32

photonomad
Member
Registered: 2005-09-10
Posts: 290
Website

Re: etc_query: all things Textpattern

UPDATE: etc_query works as expected on the front end of my site. However, it fails to insert commas when I use it in the write page on the textpattern side. (in an smd_tabber page template). Any way to make it work?

UPDATE #2: I’m using aks_article to get around this issue.

break="," isn’t working for me in the below code and I’m not sure why. I’ve been referring to examples in this forum, but I can’t figure out what I’m doing wrong.

<txp:etc_query name="hidden_articles" data="SELECT ID FROM textpattern WHERE Status=2" break="," />
<txp:variable name="hidden_articles" />

This does return a list of IDs. However, nothing separates them. Instead, it returns a list of numbers with no breaks. limit and offset do work
using: etc_query version 1.2.9 with TXP 4.5.5

Last edited by photonomad (2014-04-16 05:10:04)

Offline

#214 2014-04-18 08:38:14

etc
Developer
Registered: 2010-11-11
Posts: 5,161
Website GitHub

Re: etc_query: all things Textpattern

photonomad wrote #280257:

UPDATE: etc_query works as expected on the front end of my site. However, it fails to insert commas when I use it in the write page on the textpattern side. (in an smd_tabber page template). Any way to make it work?

Yes, because doWrap() function (used for this task) was made available on the admin side only recently (in 4.5?), and I have not yet modified the etc_query code. If necessary, just replace

$finalout = @txpinterface === 'admin' ? implode('', $out) : ($label ? doLabel($label, $labeltag) : '').doWrap($out, $wraptag, $break, $class, '', '', '', $html_id);

with

$finalout = ($label ? doLabel($label, $labeltag) : '').doWrap($out, $wraptag, $break, $class, '', '', '', $html_id);

at the end of the function etc_query(...) {...} block.

Offline

#215 2014-04-24 01:42:34

photonomad
Member
Registered: 2005-09-10
Posts: 290
Website

Re: etc_query: all things Textpattern

Thank you, etc!

Is it possible to use etc_query to get the coordinate value of the object with a specific item_id value? I need to be able to get the coordinate value via the id # – the order of the objects may change, so finding it by position won’t work. For example, how would I get the coordinates1 value of item_id value 26?

var data = {
"items": [
 {"item_id": 39,
 "coordinates1": "40.6833673, -73.9437969"
 },
 {"item_id": 38,
 "coordinates1": "35.1382740, -90.0579570"
 },
 {"item_id": 27,
 "coordinates1": "36.1514090, -86.7795840"
 },
 {"item_id": 26,
 "coordinates1": "36.1559550, -86.7735036"
 },
 {"item_id": 24,
 "coordinates1": "36.2018540, -86.7399159"
 },
 {"item_id": 23,
 "coordinates1": "36.1521753, -86.7973176"
 },
 {"item_id": 7,
 "coordinates1": "36.1739869, -86.7613520",
 "coordinates2": "36.1273830, -86.7778240"
 },
 {"item_id": 6,
 "coordinates1": "36.2300240, -86.7610170"
 },
 {"item_id": 28,
 "coordinates1": "36.0460331, -86.6756147"
 }
]}

Last edited by photonomad (2014-04-24 14:15:44)

Offline

#216 2014-04-24 10:55:24

etc
Developer
Registered: 2010-11-11
Posts: 5,161
Website GitHub

Re: etc_query: all things Textpattern

photonomad wrote #280384:

Is it possible to use etc_query to get the coordinate value of the object with a specific item_id value? I need to be able to get the coordinate value via the id # – the order of the objects may change, so finding it by position won’t work. For example, how would I get the coordinates1 value of item_id value 26?

Yes, but delete the comma after "coordinates2": "36.1273830, -86.7778240", first (invalid markup). Then call

<txp:etc_query markup="json" data='{...your json data...}'
	query="string(//item_id[text()=26]/following-sibling::coordinates1[1])" />

Offline

Board footer

Powered by FluxBB