Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
PHP and txp:output_form question
Question for a PHP pro – how to combine PHP with a call to a TXP form?
I can make the following work fine by including a PHP file, but thought I’d try and simplify the query by placing it in a form. This is what I want to do:
<txp:php>
if(array_intersect($_SESSION['_amember_product_ids'], array(1,2,3,4,5))) {
echo "<txp:output_form form="logged_in_yes" />";
else {
print "<p>You are not logged in</p>";
}
</txp:php>
The if statement checks a session variable from aMember. I’d like to print out a TXP form or display a message. How to get this to work?
Offline
Re: PHP and txp:output_form question
You can call any Txp tag directly as a function. Make the tag attributes into an associative array and use that as the argument to the function call (or use an empty array if there are no attributes needed). Hence:
output_form(array("form"=>"logged_in_yes"));
Code is topiary
Offline
Re: PHP and txp:output_form question
Mmm its late here and my eyes hurt :-) I have this now, which I am sure is wrong because it results in an error:
<txp:php>
if(array_intersect($_SESSION['_amember_product_ids'], array(1,2,3,4,5))) {
output_form(array("form"=>"logged_in_yes"));
else {
print "<p>You are not logged in</p>";
}
</txp:php>
Parse error: syntax error, unexpected T_ELSE in /users/home/xxx/textpattern/publish/taghandlers.php(3127) : eval()'d code on line 4
Offline
Offline
Re: PHP and txp:output_form question
Is there a reason why the second form does not output – the else
clause?
<txp:php>
if(array_intersect($_SESSION['_amember_product_ids'], array(1,2,3,4,5))) {
output_form(array("form"=>"logged_in_yes"));
} else {
output_form(array("form"=>"logged_in_no"));
}
</txp:php>
Offline
Re: PHP and txp:output_form question
Depending on what’s in the forms you probably still need to echo
them
Code is topiary
Offline
Re: PHP and txp:output_form question
Mmm I keep getting errors no matter what I try. My PHP is not that great :-( Would you mind posting a corrected example?
And – is it better to use this method or would it be better to extract the value of the aMember array into a TXP variable and print the form based on that?
Offline
Re: PHP and txp:output_form question
Not just echo. You’ll probably want to parse the form as well:
<txp:php>
$form = array_intersect($_SESSION['_amember_product_ids'], array(1,2,3,4,5)) ? 'yes' : 'no';
echo parse(output_form(array('form'=>'logged_in_'.$form)));
</txp:php>
Offline
Re: PHP and txp:output_form question
Because probably there isn’t always a session set, if you want to get away from notices you need a isset
check and probably session_start()
if the session hasn’t already started for the request.
if(!isset($_SESSION)) session_start();
And:
$form = (isset($_SESSION['_amember_product_ids']) and array_intersect([...]
Also, with out us knowing the exact errors, make sure that $_SESSION['_amember_product_ids']
actually contains an array. If it sometimes does not contain an array, then you need to add is_array
check too.
Btw, are you sure that you want to use array_intersect()
. Or are you just trying to compare values? As you are not going to return anything from the array. If so, wouldn’t in_array()
do the job too (and with out it re-building the array).
Last edited by Gocom (2009-12-23 12:51:01)
Offline
Re: PHP and txp:output_form question
Ruud, that is one beautiful code example. There is no way I could ever produce that – but its very clear and concise. Wow. Thanks!
Offline
Re: PHP and txp:output_form question
Hi Jukka,
There is a <txp:php> session_start(); </txp:php>
on the top of each page, so a session is set, but there may be no result from the aMember array. What it does is check if the member is:
- logged in
- currently subscribed to product 1-5
If yes, display form logged_in_yes
. If not, display form logged_in_no
.
Seems to work with Ruud’s code but I need to test some more. And – always happy to learn better ways to code. Thanks!
Offline
Pages: 1