Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#325 2017-04-05 16:34:45

jeroenvg
Member
From: netherlands
Registered: 2010-04-21
Posts: 34

Re: smd_query: Talk to the database directly via SQL

Bloke wrote #305214:

I’m just curious, where is the field article_id in your setup?

in a Form (type Article), called from an Article (see #304909, #305030).

from the sentence in the plugin’s help:

“Specify the field name with a ? in front of it (e.g. query=“SELECT * FROM txp_image WHERE category=’?category1’ OR category=’?category2’) would show images that had their category set to one of the article’s categories.”

…i understood that i need to rewrite:

<txp:category1 />

…as:

?category1

…to use its value in a smd_query attribute.

Offline

#326 2017-04-05 17:30:12

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

Re: smd_query: Talk to the database directly via SQL

jeroenvg wrote #305229:

Specify the field name with a ? in front of it…

That’s correct. Look at the database table textpattern. Any field (column) there is fair game, e.g. Title, Body_html, custom_1, url_title, ID, category1, and so forth. But there’s no column called article_id so ?article_id won’t match anything and will thus shrug and return the string ‘article_id’ as nothing matched and the plugin doesn’t know what to do with it.

The only time it would match anything (which spurred my question about whether you’d defined it yourself somewhere… though I didn’t articulate that very well, sorry) is if you’d previously done this:

<txp:variable name="article_id" value="480" />

or, more usefully:

<txp:variable name="article_id" value='<txp:article_id />' />

But if you’re doing that and not reusing the variable, you might as well just stick to your workaround and use the tag inside the smd_query attribute directly.

Does that make sense?


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

#327 2017-04-07 14:47:16

jeroenvg
Member
From: netherlands
Registered: 2010-04-21
Posts: 34

Re: smd_query: Talk to the database directly via SQL

Bloke wrote #305232:

Does that make sense?

yip, thanks!

it was category1 in your example that got me off the wrong foot. i understood you ‘rewrote the textpattern tag in question mark form’, but it was the table column label Category1 you were referring to.

Offline

#328 2018-05-21 18:00:35

jeroenvg
Member
From: netherlands
Registered: 2010-04-21
Posts: 34

Re: smd_query: Talk to the database directly via SQL

does smd_query cache results?

i’ve built a plug-in to take reservations for workshops. on a workshop page (an article) for any workshop, it

  1. lists <vacant> places of <total> places – a counter,
  2. takes a reservation by asking for name and e-mail address – an (html) form.

reservations are, among other result actions, stored in a table in the database.

the counter (1) is a (txp) Form my plug-in created, that uses smd_query to read the current score from the database table.

the form (2) is a (txp) Form my plug-in created, that uses zem_contact_reborn for form flow, and adi_contact to store the reservation.

the problem
after a successful form submit, on the ‘thank you’ page, the counter is always 1 behind, or – put another way: it did not count the reservation that just happened, or – again in other words: it seems that smd_query did not run again to get the latest score from the database, though the ‘thank you’ page is a new/ changed (not-cached) page (it shows the ‘thank you’ message).

this leads to some customers repeating their reservation, because they did the math too and think something went wrong. the workshop shows the correct score however, if they’d browse away and navigate back to the page.

this is how i tried to fix it

  • set $prefs['send_lastmod'] = false; at the start of my plug-in, for pretext_end event.
  • from my plug-in, hooked into zemcontact.deliver event on the public side, to run update_lastmod(). (this sets lastmod in table txp_prefs to <now>).
  • copied zem_contact_reborn.php to my plugin directory, and changed the Last-Modified header in zem_contact() from one week ago to <now>.
  • checked that ‘thank you’ page never loads from a browser memory or disk cache (true).
  • searched smd_query.php for caching functions (can’t find any).

i’m running out of solutions. anything you can think of?

context

  • Preferences: Site: Production status: Live
  • Preferences: Publish: Send “Last-Modified” header: No
  • zem_contact_reborn 4.5.0.0-beta.4
  • smd_query 0.50
  • txp 4.6.2
  • php 5.5.9

Offline

#329 2018-05-22 08:11:54

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

Re: smd_query: Talk to the database directly via SQL

If you’re reading from a table then you’ll hit this issue if you reload the same page and the content isn’t yet updated. You can force fetch the content by hand first into a variable and use that in your form. Or try changing the load order of the plugins?


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

#330 2018-06-26 15:41:18

jeroenvg
Member
From: netherlands
Registered: 2010-04-21
Posts: 34

Re: smd_query: Talk to the database directly via SQL

Bloke wrote #311954:

If you’re reading from a table then you’ll hit this issue if you reload the same page and the content isn’t yet updated.

checked that. i happen to have created and modified timestamps in the table i’m saving to. created is a value the form generated. modified an automatic timestamp for the last time the contents of the table row changed. i can easily compare these to a timestamp php writes to the page, from code in the Form.

if i make the html form sleep for 10 seconds after it saved the values to the database, and before the page reloads to show the counter and ‘thank you’ message again, it still shows the wrong (not updated) count.

Or try changing the load order of the plugins?

tried that too, with all combinations of ‘earlier’/ ‘later’ for smd_query, zem_contact_reborn, adi_contact and my plugin. but that doesn’t fix it.

Offline

#331 2018-06-26 15:51:02

jeroenvg
Member
From: netherlands
Registered: 2010-04-21
Posts: 34

Re: smd_query: Talk to the database directly via SQL

jeroenvg wrote #311942:

does smd_query cache results?

think i found what’s going on. from looking at mysql query log, i can see smd_query asking for the current score from my table when the page with html form is rendered. but for the (new, uncached) ‘thank you’ page, the query is not repeated.

so, the ‘thank you’ page gets the counter value somewhere else. could it be that they’re carried over from the html form?

if so, is there a way to clear smd_query values beforehand, to make the ‘thank you’ page query my table again?

Offline

#332 2018-08-31 08:28:40

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

Re: smd_query: Talk to the database directly via SQL

I’m back in the land of Textpattern after a long break!

I have a page that returns results from the database – in this case, a list of incidents. I have a form that allows the user to filter the list according to the type of incident – a fire, a community event or a controlled burn. Or everything. The form posts the selection in a URL variable called ‘type’.

Here’s my code so far:

<txp:smd_query query="SELECT id, date_format (date_out,'%d/%m/%Y') as date, type, description, location FROM gck_incidents where type like '?type' order by date_out desc" limit="25" urlfilter="/(\ball\b)/" defaults="type:%" pageform="page_info"> 

This works perfectly for any selected value, but if the user leaves the value as the default of ‘all’ then nothing is returned. I’m still trying to figure out regular expressions so I know this needs more work to stop users adding anything else they want to the URL. But for testing purposes it would be good to at least get this working.

Last edited by aslsw66 (2018-08-31 08:29:20)

Offline

#333 2019-12-30 11:04:18

demoncleaner
Plugin Author
From: Germany
Registered: 2008-06-29
Posts: 220
Website

Re: smd_query: Talk to the database directly via SQL

Just in case the world is not aware of it… I just found out that this beautiful peace of code will unfortunately not work anymore under textpattern 4.8. (beta) It will just output nothing.

Offline

#334 2019-12-30 13:26:56

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,137
GitHub

Re: smd_query: Talk to the database directly via SQL

demoncleaner wrote #320759:

I just found out that this beautiful peace of code will unfortunately not work anymore under textpattern 4.8. (beta) It will just output nothing.

I am sure Stef (bloke) will be able to make it work again, he’s that kind of guy.

Offline

#335 2019-12-30 15:07:28

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

Re: smd_query: Talk to the database directly via SQL

The plugin tag just needs registering. I can do that and release an update as soon as I can, but if you want to do it yourself in the meantime, see the tag registering example.


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

#336 2020-01-02 16:20:54

demoncleaner
Plugin Author
From: Germany
Registered: 2008-06-29
Posts: 220
Website

Re: smd_query: Talk to the database directly via SQL

Perfect. Will have a look at it. Thank you so much!

Offline

Board footer

Powered by FluxBB