Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2023-02-26 19:38:51

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

Unset variables in a loop

Let’s say I’m doing something in a Form that gets called time and again for every article, and that form sets a bunch of variables read from an external source:

<txp:php>
if (!empty($source['var1')) {
   <txp:variable name="var1" value="$source['var1')" />
}
if (!empty($source['var2')) {
   <txp:variable name="var2" value="$source['var2')" />
}
...
</txp:php>

If the first value is set in the first record, great. But if in the second record, the first var is not set then it’ll still be set from the previous iteration. This leads to errors.

Easy fix: just set every potential variable to "" before each run:

<txp:variable name="var1" value="" />
<txp:variable name="var2" value="" />
...

Except that means they’re still ‘set’ and therefore they still exist so when we come test them later:

<if::variable name="var1">... do something</if::variable>

it gets output. One (possibly wordy) solution is to test against not empty:

<if::variable name="var1" not value="">... do something</if::variable>

But that means on the very first run (when no variables have actually been defined) that the first record fails the not value="" test. So we need to test both if the value is set at all OR if it’s empty – because this form is called from a few different pages. So I’d need to make sure the variables were all initialised in the form itself, I guess.

All that got me wondering if there’s a way we can actually unset variables so they get removed, short of resorting to unset($variable['var1']) in PHP, which is not exactly futureproof.

Can we do that somehow? Or is the extended ‘not value’ test the only way out of this loop?


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

#2 2023-02-26 23:33:43

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

Re: Unset variables in a loop

Bloke wrote #334778:

All that got me wondering if there’s a way we can actually unset variables so they get removed, short of resorting to unset($variable['var1']) in PHP, which is not exactly futureproof.

All I can say is that reset attribute should work:

<txp:variable name="var1" reset output="" />

Without output="", the variable will be output before being unset.

Offline

#3 2023-02-26 23:34:47

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

Re: Unset variables in a loop

D’oh of course. I’ll give that a whirl, thank you.


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

#4 2023-02-27 02:00:19

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

Re: Unset variables in a loop

That seems to have worked a treat. Thank you.


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