Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2009-05-05 06:52:08

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

Re: smd_random_text: Pull random items from the TXP environment

the_ghost wrote:

And i want to make this random text to be stable :)

Bizarrely I had a similar requirement recently and had to “fix” it with some PHP. If you look at my contact page the “Favourite question” at the bottom is chosen using smd_random_text. The problem I had was that if you submit a “bad” form (i.e. one that showed a zemError) then it would generate a new ‘favourite’ each time. I circumvented this with a SESSION variable and ‘forced’ the random item by setting a txp:variable in advance of the smd_random_text call.

I like your idea of fixed randomness though. It may be that I can address both your needs and mine with a new attribute and some cunning. Leave it with me and I’ll look into it.


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

Online

#14 2009-07-26 17:20:30

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

Re: smd_random_text: Pull random items from the TXP environment

Minor update to add a couple of attributes and a new replacement tag. The first is to ‘fix’ the randomness which is mighty handy if you want to link some disparate but related sources of info together. Not quite what the_ghost was after but it might be able to be abused for this purpose. If not I can have a go at adding what you wanted at a later date.

The second attribute allows a subsequent bout of randomness to be introduced if you need it.

Download v0.12


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

Online

#15 2010-07-10 08:43:08

dandul
Member
From: Brisbane, Australia
Registered: 2010-01-31
Posts: 33

Re: smd_random_text: Pull random items from the TXP environment

Hiya

I’m trying to set up a random quote using a text file as the source.

The code is:
<div id="quote"> <h3>Dharma quote</h3> <txp:smd_random_text type="file" source="files/VGKG-quotes-pick.txt" form="330_quote" limit="1" /> </div>

And the “330_quote” looks like this:
{smd_rnd_txt}

The h3 heading appears but no quote. The text file is located in the “files” folder and I’m running txp 4.2. Any suggestions?


Stop wishing things were different to the way they are. Accept it, deal with it, move on.

Offline

#16 2010-07-10 20:02:52

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

Re: smd_random_text: Pull random items from the TXP environment

dandul wrote:

The text file is located in the “files” folder

A few things you could try/check:

  1. Try it with a / before your files directory name — it might just not be finding the file path correctly
  2. Is your site in a subdirectory?
  3. Are your random quotes in the text file delimited by a newline?
  4. Are your newlines “real” newlines or Windoze-style (or Mac-style) newlines?
  5. Add debug="1" (or maybe 2 as well) and see if that gives you any clues. If not, you can always post the relevant stuff here and I’ll see if I can figure it out

If (3) or (4) are the cause you might be able to alter the file_delim attribute to make it work properly. I’ve not tried it with non-UNIX line endings. You could always smoosh them all onto one line and use a different delimiter if it’s not working right — depending on how many quotes you have in total.


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

Online

#17 2010-07-10 20:37:45

the_ghost
Plugin Author
From: Minsk, The Republic of Belarus
Registered: 2007-07-26
Posts: 907
Website

Re: smd_random_text: Pull random items from the TXP environment

dandul wrote:

The h3 heading appears but no quote. The text file is located in the “files” folder and I’m running txp 4.2. Any suggestions?

Try to check the url – try absolute url instead of relative – /files/... instead of files/...


Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?

Offline

#18 2010-07-11 11:46:26

dandul
Member
From: Brisbane, Australia
Registered: 2010-01-31
Posts: 33

Re: smd_random_text: Pull random items from the TXP environment

Doh! I forgot to mention i was testing on my laptop. i uploaded it to a live site and it works fine. Should have tried that before posting. Ah well, it works, thanks everyone


Stop wishing things were different to the way they are. Accept it, deal with it, move on.

Offline

#19 2010-07-16 07:27:00

otti
Member
From: Australia
Registered: 2008-04-16
Posts: 75
Website

Re: smd_random_text: Pull random items from the TXP environment

I have implemented smd_random_text successfully on my website. It pulls random quotes from a database, but not without a problem. Once a page gets refreshed (reloaded), the previous randomly selected quote stays as is and no new quote gets selected. Has this something to do with the cache in the browser? As soon as I clear the cache in the browser and refresh the page, a new quote gets displayed. A page refresh only reloads possibly one new quote and then gets stuck with it.
Many thanks for any help.
otti

Offline

#20 2010-07-16 08:08:18

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

Re: smd_random_text: Pull random items from the TXP environment

otti wrote:

Has this something to do with the cache in the browser?

Yes. Try visiting Admin->Advanced Prefs and setting Send “last modified” header to No.

I could probably implement a feature that added a random class or something to the quote (if you used wraptag) to try and defeat the cacheing. But in theory the fact that the quote is changing each time should be enough to trigger ‘different from last time’ behaviour. I don’t know why it doesn’t do this; I’ve noticed it myself sometimes.


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

Online

#21 2011-02-28 16:24:37

giampablo
Member
From: Italy
Registered: 2008-07-17
Posts: 86
Website

Re: smd_random_text: Pull random items from the TXP environment

Hi, I am trying to use a TXP article with user comments as quotes. It is a testimonial type thing. I do not want to hide the article, since it is used to ask for comments/quotes.
In a different Section the plug-in should show a random quote AND the commenter’s name.

As a start I tried to use this, as per example7 on help and it is not working:

<txp:smd_random_text type="database"
       source="txp_discuss|message" escape="" >
       {smd_rnd_txt}
       <txp:smd_random_text type="database" 
       source="txp_discuss|name"
       item_num="{smd_rnd_txt_chosen_item}" />
</txp:smd_random_text>

What I’m doing wrong? And how can I grab the quote with associated author from the article instead of the database?

Thanks for any help.
I am on txp 4.3 and PHP 5.2.4

Offline

#22 2011-02-28 22:23:01

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

Re: smd_random_text: Pull random items from the TXP environment

giampablo wrote:

What I’m doing wrong?

*cough* absolutely nothing. The problem is the plugin, sorry. That example 7 was a crock. I’ve just revamped the internals of the plugin in v0.13 for you so that you can do things properly and can pull multiple columns from the database in one call.

How’s this:

<txp:smd_random_text type="database"
     source="txp_discuss|message,name" escape="">
   <p><cite>{smd_rnd_txt_name}</cite> wrote:</p>
   <blockquote>
   {smd_rnd_txt_message}
   </blockquote>
</txp:smd_random_text>

It’s a shedload more efficient and allows you to do a lot more. Sorry for the inconvenience, hope this version allows you to get where you want to be.


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

Online

#23 2011-03-01 17:44:41

giampablo
Member
From: Italy
Registered: 2008-07-17
Posts: 86
Website

Re: smd_random_text: Pull random items from the TXP environment

Almost … perfect, Stef!

The plugin works like a charm, but if I use something like <txp:article_custom id=“my_article_ID”> it still pulls multiple columns from the whole table, not from THAT article only.
Any idea?

By the way, thanks for the fast answer and the revamped plugin.

Offline

#24 2011-03-01 20:28:33

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

Re: smd_random_text: Pull random items from the TXP environment

giampablo wrote:

if I use something like <txp:article_custom id=“my_article_ID”> it still pulls multiple columns from the whole table, not from THAT article only.

It will. The plugin isn’t clever enough to figure out that it should restrict itself to the current article. Although you can get any fields from the current context (file, image, link, article) so you could for example get the ID of the current article by using type="field" source="thisid" (although you might as well just use <txp:article_id />!) the fact that the comments attached to the current article are in a different table means you can’t join the data with this plugin.

You could however do something like this:

  1. Get the article ID of the current article and store it in a txp:variable
  2. Use smd_random_text to get all comments from the database — they’ll come back sorted randomly. Make sure you grab name, message, parentid at a minimum
  3. Inside the container use txp:if_variable to check if the parentid value matches the current article. If it does, output the {smd_rnd_txt_message} and name. If not, keep going until you do find one.

The only slight issue will be that you will need to drop out of the smd_random_text loop upon finding the testimonial. Best way to do that is probably by setting another txp:variable up front to use as a flag and only execute the container if the value is 0. When you find the testimonial and have output it, set the flag to 1 to kill any further output.

Alternatively, save yourself some hassle and use smd_query instead :-)


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

Online

Board footer

Powered by FluxBB