Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-12-22 21:19:49

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

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

#2 2009-12-22 22:04:05

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

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

#3 2009-12-22 22:14:04

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

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

#4 2009-12-22 22:19:23

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: PHP and txp:output_form question

Doh. Missing a } before the else. Double doh.

Offline

#5 2009-12-22 22:22:27

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

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

#6 2009-12-22 23:29:43

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

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

#7 2009-12-23 08:51:12

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

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

#8 2009-12-23 11:04:47

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

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

#9 2009-12-23 12:39:46

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

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

#10 2009-12-23 15:30:42

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

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

#11 2009-12-23 15:35:30

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

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:

  1. logged in
  2. 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

Board footer

Powered by FluxBB