Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#211 2014-03-22 12:08:39
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
Offline
#213 2014-04-16 02:19:32
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
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
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
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
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
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
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
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
#221 2014-10-30 14:03:14
Re: etc_query: all things Textpattern
I struggle to remember all of the great plugins out there
Me too, especially after being away from things for a while.
preg_replace is also doable with smd_wrap and the transform="replace|regex|..." attribute. I also struggle with regex, but there are various helper sites (and apps like patterns for the mac or a similar regex tool for windows) that make life easier. Oleg’s pattern first of all looks only in href attributes, courtesy of his plugin and then does the following:
- The
/ … /just delimit the pattern to search for. - starting with a
#, then search for.*= zero or more single characters until$= the end of the string is reached.
TXP Builders – finely-crafted code, design and txp
Offline
#222 2014-10-30 17:18:37
Re: etc_query: all things Textpattern
aslsw66 wrote #285228:
I did play around with
preg_replacebut frankly it feels like black magic to me – it’s very powerful but highly codified.
Actually, preg_replace is too much here, a simpler (and more comprehensive) option would be
<txp:etc_query data="<a href='www.domainname.com/folder/index.php?query1=value1&query2=value2#hashvalue'>link</a>"
replace="//a@@href={substring-before(string(@href), '#')}" />
but I have discovered that XPath Edit: no, it is some string() function breaks on the first & (amp) character, so neither will work for your example.etc_query bug, the snippet above should work.
It looks like I have three options to play with!
It depends on where URL comes from…
Last edited by etc (2014-10-30 20:12:49)
Offline
#223 2015-01-14 18:15:48
Re: etc_query: all things Textpattern
If I could wrap my head around the power of this tool I’m sure I could figure this out. Until then I have to ask for help.
I’m pulling data from a Google Calendar XML feed but the summary of the events is a big messy html like this
When: Fri Feb 6, 2015 6pm to 10:30am
HST<br>
<br>Who: John Doe, The Amazing Restaurant Events, Jane Doe
<br>Where: The Amazing Restaurant
<br>Event Status: tentative
<br>Event Description: Come exercise your brain with the BEST weekly Trivia...
How can I not print the Who line and the Event Status line? Or even better is there a way to separate each of the lines and selectively output them?
Recurring events are even worse!
Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker
Offline
#224 2015-01-14 18:33:50
Re: etc_query: all things Textpattern
From the top of my head, you can output the text following, say, the 3rd <br> with
<txp:etc_query ...>
{//br[3]/following-sibling::text()[1]}
</txp:etc_query>
But yes, it’s a real mess…
Offline
#225 2015-01-15 10:41:01
Re: etc_query: all things Textpattern
You might have some difficulties with Google Calendar atom feed: it’s a namespaced XML, some parts of content are html encoded, and, when decoded, contain unclosed <br>, invalid in XML. I have managed to import it with
<txp:etc_query markup="xml:"
url="https://www.google.com/calendar/feeds/YOUR_ID@group.calendar.google.com/public/basic"
query="//entry"
replace="content={str_replace('<br>', '<br />', html_entity_decode(htmlspecialchars_decode(string())))}"
functions="htmlspecialchars_decode,html_entity_decode,str_replace"
>
{content/br[3]/following-sibling::text()[1]}
</txp:etc_query>
fwiw
Offline