Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

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

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,479
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.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#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: 12,479
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.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#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: 12,479
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.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#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: 12,479
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.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#25 2011-04-24 06:46:42

MyOtheHedgeFox
Member
From: Russia
Registered: 2010-11-06
Posts: 10
Website

Re: smd_random_text: Pull random items from the TXP environment

Erm, Stef, sorry for the weird question I want to ask, but is it possible to use more-than-one-symbol delimiters (like \n|\n) when parsing textfiles? Could really help with parsing “database” textfiles prepared for fortune-mod or for some other programs that make use of random quotes.
And \n|\n is, after all, less common than [*|*], so it can be used as a failproof measure.

Last edited by MyOtheHedgeFox (2011-04-24 06:47:38)

Offline

#26 2011-04-26 14:09:20

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,479
Website GitHub

Re: smd_random_text: Pull random items from the TXP environment

MyOtheHedgeFox wrote:

is it possible to use more-than-one-symbol delimiters (like \n|\n) when parsing textfiles?

Never tried it, but in theory yes. It should split by whatever character(s) you give it. If you’ve tried it and it’s not working, let me know what you tried and how you set your tag / files up: I’ll try it out the same as you have it and, if it doesn’t work, look into a fix.

Last edited by Bloke (2011-04-26 14:09:37)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#27 2011-04-26 14:16:03

MyOtheHedgeFox
Member
From: Russia
Registered: 2010-11-06
Posts: 10
Website

Re: smd_random_text: Pull random items from the TXP environment

Actually, I didn’t try it yet (woe upon me the newbie from Rashah): that’s why I actually chose to ask you first. Will try.

Offline

#28 2011-04-29 07:04:36

MyOtheHedgeFox
Member
From: Russia
Registered: 2010-11-06
Posts: 10
Website

Re: smd_random_text: Pull random items from the TXP environment

Alrighty, parsing the Discworld fortune database didn’t do what I expected.

The database homepage: http://www.splitbrain.org/projects/fortunes/discworld

My code:

<txp:smd_random_text type="file" source="files/discworld.txt" file_delim="\n%\n"><div class="clearing">{smd_rnd_txt}<br />Rows: {smd_rnd_txt_rows}<br />This is a row number {smd_rnd_txt_thisrow}</div></txp:smd_random_text>

(I’ve renamed discworld into discworld.txt)

results in:

It was all very well going about pure logic and how the universe was ruled by logic and the harmony of numbers, but the plain fact was that the disc was manifestly traversing space on the back of a giant turtle and the gods had a habit of going round to atheists' houses and smashing their windows.
Rows: 1
This is a row number 1

Offline

#29 2011-04-29 07:13:46

MyOtheHedgeFox
Member
From: Russia
Registered: 2010-11-06
Posts: 10
Website

Re: smd_random_text: Pull random items from the TXP environment

That’s even funnier. Attempts to parse othe fortune databases result in only the first lines of the textfiles.
In example, parsing this file results in:

Воспитание - это все. Персик был когда-то горьким миндалем, а цветная

which is the first line of the text.

Tried removing \n’s from the delimiter rules. Didn’t help.

Added later:

Now that’s funny. =)

Made a test textfile for parsing and reformed it like this:

A little pain never hurt anyone.|Christ was born in 4 B.C.|Cum tacent, clamant. When they are silent, they shout. -Cicero|Goes (Went) over like a lead balloon.

This way and with the standard delimiter ([**|**]), it randomizes well.

But when I try arranging the items like this:

A little pain never hurt anyone.

Christ was born in 4 B.C.

Cum tacent, clamant. When they are silent, they shout. -Cicero

Goes (Went) over like a lead balloon.

and set the delimiter to \n\n, it results in outputting only the very top string.

Also, though it’s an expected result, such file:

A little pain never hurt anyone.
Christ was born in 4 B.C.
Cum tacent, clamant. When they are silent, they shout. -Cicero
Goes (Went) over like a lead balloon.

with \n as a delimiter is parsed correctly.
Looks like the plugin doesn’t support multiple-symbols delimiters.

Last edited by MyOtheHedgeFox (2011-04-29 07:26:31)

Offline

#30 2011-05-03 00:05:13

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,479
Website GitHub

Re: smd_random_text: Pull random items from the TXP environment

MyOtheHedgeFox wrote:

Looks like the plugin doesn’t support multiple-symbols delimiters.

A correct assertion: a stupid bug prevented their use. But not any more. Enter v0.14 which permits regular expressions in file_delim. You do have to tell it of your intentions by specifying file_delim_type="regex" but that’s the only overhead. After that you will be able to do what you expect, namely:

<txp:smd_random_text
     type="file"
     source="files/discworld.txt"
     file_delim="\n%\n"
     file_delim_type="regex" />

If you don’t tell it that you are using a regex then it will try to split the file contents using the verbatim string that you supply as a file_delim. While this might seem like it would work, the idiosyncracies of PHP’s explode() function means that a character sequence that incorporates control chars (like \n, \t, etc) are treated as a literal sequence of the characters ‘backslash-n’ and so forth, instead of ‘newline’. Permitting regexes also opens up the possibility of creating some pretty interesting file structures!


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB