Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#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: 9,723
Website

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

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: 9,723
Website

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

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: 9,723
Website

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.

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: 9,723
Website

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.

Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB