Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2018-02-27 14:51:54

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

Using random numbers in Textpattern variables

Take the following code:

<txp:variable name="test-rng" value="<txp:php>echo rand(1,999);</txp:php>" />

<txp:variable name="test-rng" /> <txp:variable name="test-rng" /> <txp:variable name="test-rng" /> <txp:variable name="test-rng" />

That creates a variable called test-rng, and assigns the result of echo rand(1,999);, which is a random integer between 1 and 999, and then outputs the variable test-rng four times.

In each case, the variable test-rng gives a different integer between 1 and 999 (for example: 437, 21, 993, 487).

What I would really like to do is set a variable with the result of echo rand(1,999);, and lock that number in until it’s reset or unset later in the code.

Is such a thing possible? How would I achieve this?

Thank you in advance!

Offline

#2 2018-02-27 14:59:00

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

Re: Using random numbers in Textpattern variables

How about:

<txp:variable name="test-rng"><txp:php>
    static $a_random_number = null;

    if ($a_random_number === null) {
        $a_random_number = rand(1,999);
    }

    echo $a_random_number;
</txp:php></txp:variable>

EDIT: That might not allow you to easily unset later in the code, though. Statics can be quite persistent.

Last edited by Bloke (2018-02-27 15:02:11)


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

#3 2018-02-27 15:00:10

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,032
Website GitHub Mastodon Twitter

Re: Using random numbers in Textpattern variables

Hi Pete,

I’m not good with php but I am just about Ok with txp:) Did you try the following?

<txp:variable name="test-rng" value='<txp:php>echo rand(1,999);</txp:php>' />

>Edit… Stef beat me to it:) Almost certainly with the proper solution.

Last edited by colak (2018-02-27 15:02:13)


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#4 2018-02-27 15:10:44

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

Re: Using random numbers in Textpattern variables

Hey colak – thank you, I had already tried the single and double quotes swap, no luck with that.

Thanks Stef – worked perfectly, and the static can be unset easily enough:

<txp:variable name="test-rng"><txp:php>
    static $a_random_number = null;

    if ($a_random_number === null) {
        $a_random_number = rand(1,999);
    }

    echo $a_random_number;
</txp:php></txp:variable>

<p>Blokemagic&copy;</p>

<txp:variable name="test-rng" /> <txp:variable name="test-rng" /> <txp:variable name="test-rng" />

<p>unset here</p>

<txp:variable name="test-rng" value=""/>

<txp:variable name="test-rng" /> <txp:variable name="test-rng" /> <txp:variable name="test-rng" />

<p>echo rand() here</p>

<txp:variable name="test-rng" value="<txp:php>echo rand(1,999);</txp:php>"/>

<txp:variable name="test-rng" /> <txp:variable name="test-rng" /> <txp:variable name="test-rng" />


<txp:variable name="test-rng"><txp:php>
    static $a_random_number = null;

    if ($a_random_number === null) {
        $a_random_number = rand(1,999);
    }

    echo $a_random_number;
</txp:php></txp:variable>

<p>reset to Blokemagic&copy;</p>

<txp:variable name="test-rng" /> <txp:variable name="test-rng" /> <txp:variable name="test-rng" />

…which outputs:

Blokemagic©

486 486 486
unset here

echo rand() here

282 821 933
reset to Blokemagic©

462 462 462

Invoice in the post to the usual address, please and thanks.

Offline

#5 2018-02-27 15:36:52

etc
Developer
Registered: 2010-11-11
Posts: 5,082
Website GitHub

Re: Using random numbers in Textpattern variables

gaekwad wrote #309472:

Hey colak – thank you, I had already tried the single and double quotes swap, no luck with that.

Yiannis code works for me:

<txp:variable name="test-rng" value='<txp:php>echo rand(1,999);</txp:php>' />

<txp:variable name="test-rng" />
<txp:variable name="test-rng" />
<txp:variable name="test-rng" />

And no need to complicate with statics, this works as well

<txp:variable name="test-rng">
    <txp:php>echo rand(1,999);</txp:php>
</txp:variable>

For the record, with double quotes in

<txp:variable name="test-rng" value="<txp:php>echo rand(1,999);</txp:php>" />

every <txp:variable name="test-rng" /> call will output the literal <txp:php>echo rand(1,999);</txp:php> string, which will be processed on the second pass. A nice usecase, BTW.

Offline

#6 2018-02-27 15:41:12

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

Re: Using random numbers in Textpattern variables

etc wrote #309473:

Yiannis code works for me:

Yiannis – I apologise – your code does work. I must have mental wires crossed somewhere this afternoon. Very sorry!

etc wrote #309473:

And no need to complicate with statics, this works as well

<txp:variable name="test-rng">...

For the record, with double quotes in

<txp:variable name="test-rng" value="<txp:php>echo rand(1,999);</txp:php>" />...

every <txp:variable name="test-rng" /> call will output the literal <txp:php>echo rand(1,999);</txp:php> string, which will be processed on the second pass. A nice usecase, BTW.

Thank you, Oleg – very much appreciated!

Offline

#7 2018-02-27 15:53:16

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

Re: Using random numbers in Textpattern variables

Nice! Didn’t realise that would occur, thanks Oleg. And the second pass feature is neat. I almost went with some clever use of second pass/output_form/evaluate to allow Pete to ‘reset’ the value without having to redeclare it.

I’m sure it’s possible to pass in an attribute that’ll “seed” it with a random value and then on subsequenet calls, pass in something else that will always “return” the set value until it’s seeded again.


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