Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

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

etc
Developer
Registered: 2010-11-11
Posts: 3,780
Website

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: 741

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: 287
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: 3,780
Website

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: 287
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: 3,780
Website

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

#217 2014-10-29 13:09:24

aslsw66
Member
From: Canberra, Australia
Registered: 2004-08-04
Posts: 342
Website

Re: etc_query: all things Textpattern

I have a URL in the format www.domainname.com/folder/index.php?query1=value1&query2=value2#hashvalue.

What I want to be able to do is reproduce this URL without the final hash value eg. www.domainname.com/folder/index.php?query1=value1&query2=value2 .

I get a sense that etc_query should be able to do this sort of thing, but I’m not clever enough to know if its true or not. I’ve been researching into XPath, but while I can see that text can be searched for it’s not clear how to either delete it completely, or end the string at the point where the text is found. It’s annoying to admit this, because I could make this work in programming languages.

Any ideas?

Offline

#218 2014-10-29 15:43:22

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 3,897
Website

Re: etc_query: all things Textpattern

You should be able to get this bit ?query1=value1&query2=value2 without the hash with this:

<txp:php>echo $_SERVER['QUERY_STRING'];</txp:php>

… and then reconstruct the rest with txp:site_url, txp:section etc. You might also be able to get this via txp:page_url like you can request_uri, but I’m not sure.

And if it’s not a txp url, you can use

<txp:smd_wrap transform="split|#||first">
  www.domainname.com/folder/index.php?query1=value1&query2=value2#hashvalue
</txp:smd_wrap>

or rah_function and the php split function to lop off the hash at the end of the string…


TXP Builders – finely-crafted code, design and txp

Offline

#219 2014-10-29 20:36:22

etc
Developer
Registered: 2010-11-11
Posts: 3,780
Website

Re: etc_query: all things Textpattern

aslsw66 wrote #285212:

I have a URL in the format www.domainname.com/folder/index.php?query1=value1&query2=value2#hashvalue.

What I want to be able to do is reproduce this URL without the final hash value eg. www.domainname.com/folder/index.php?query1=value1&query2=value2 .

I presume this URL is the value of some href attribute inside a chunk of HTML code (otherwise Jakob’s advice is probably easier to follow). You can try this:

<txp:etc_query data="your html code" functions="preg_replace"
	replace="//a/@href={preg_replace('/#.*$/', '', string())}" />

It’s actually more php than xpath.

Offline

#220 2014-10-29 21:24:20

aslsw66
Member
From: Canberra, Australia
Registered: 2004-08-04
Posts: 342
Website

Re: etc_query: all things Textpattern

And if it’s not a txp url, you can use

It’s likely to become more complex than a straight TXP URL as this will be driving some admin functionality at the front-end for specific users.

But Jakob thanks for the pointers to smd_wrap and rah_function. As usual, now you point them out I knew they were out there but also, as usual, I struggle to remember all of the great plugins out there.

It’s actually more php than xpath.

I did play around with preg_replace but frankly it feels like black magic to me – it’s very powerful but highly codified. Can I admit to tip-toeing around etc_query too? So thanks for the tip.

It looks like I have three options to play with!

Offline

Board footer

Powered by FluxBB