Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2008-09-15 07:26:46
- azw
- Member
- Registered: 2007-01-29
- Posts: 279
Problem with txp:php and a switch already returning the default value
I have a custom field “doc_type” that has values like “Annual Unit Report” and “Monthly Unit Report”.
When I use a switch on a txp form template, the value returned is always the default.
Here’s the code:
<txp:php>
$doc_type = "<txp:custom_field name="doc_type" />";
switch ($doc_type)
{
case "Annual Unit Report":
$icn = 'purple';
echo "color, doc_type: " . $icn . ", " . $doc_type;
break;
case "Monthly Unit Report":
$icn = 'blue';
echo "color, doc_type: " . $icn . ", " . $doc_type;
break;
case "Weekly Bulletin":
$icn = 'green';
echo "color, doc_type: " . $icn . ", " . $doc_type;
break;
case "Daily Situation Summary":
$icn = 'yellow';
echo "color, doc_type: " . $icn . ", " . $doc_type;
break;
case "Daily War Room Journal":
$icn = 'orange';
echo "color, doc_type: " . $icn . ", " . $doc_type;
break;
case "Daily Newsletter":
$icn = 'red';
echo "color, doc_type: " . $icn . ", " . $doc_type;
break;
default:
$icn = 'white';
echo "color, doc_type: " . $icn . ", " . $doc_type;
break;
}
</txp:php>
I’ve got the same problem with an if/elseif sequence:
<txp:php>
$doc_type = "<txp:custom_field name="doc_type" />";
if ($doc_type == 'Annual Unit Report')
{
$icn = 'purple';
echo "color, doc_type: " . $icn . ", " . $doc_type;
}
elseif (doc_type == 'Monthly Unit Report')
{
$icn = 'blue';
echo "color, doc_type: " . $icn . ", " . $doc_type;
}
elseif (doc_type == 'Weekly Bulletin')
{
$icn = 'green';
echo "color, doc_type: " . $icn . ", " . $doc_type;
}
elseif (doc_type == 'Daily Situation Summary')
{
$icn = 'yellow';
echo "color, doc_type: " . $icn . ", " . $doc_type;
}
elseif (doc_type == 'Daily War Room Journal')
{
$icn = 'orange';
echo "color, doc_type: " . $icn . ", " . $doc_type;
}
elseif (doc_type == 'Daily Newsletter')
{
$icn = 'red';
echo "color, doc_type: " . $icn . ", " . $doc_type;
}
else
{
$icn = 'white';
echo "color, doc_type: " . $icn . ", " . $doc_type;
}
</txp:php>
This code always echoes: “color, doc_type: white, …” (and whatever the doc_type is). It has the correct value for doc_type, but never has anything but white for $icn.
This must be simple problem. What am I doing wrong?
Offline
Re: Problem with txp:php and a switch already returning the default value
Try $doc_type = parse('<txp:custom_field name="doc_type" />');
Offline
Re: Problem with txp:php and a switch already returning the default value
Hi
Your error is in <txp:custom_field name=“doc_type” />
To call a txp function under txp:php you must use without a txp part :
$doc_type = custom_field (array(
'name' => 'doc_type'
));
Should work
NB: Wet is quicker and his solution is more elegant (he he it s a txp dev ;) )
Last edited by Dragondz (2008-09-15 07:53:14)
Offline
Re: Problem with txp:php and a switch already returning the default value
Change the tag <txp:custom_field name="doc_type" />
, to it’s respective function. So we get custom_field(array('name' => 'doc_type'))
.
Edit. Dang, wet and Dragon were faster :P
Last edited by Gocom (2008-09-15 07:55:15)
Offline
#5 2008-09-15 08:06:10
- azw
- Member
- Registered: 2007-01-29
- Posts: 279
Re: Problem with txp:php and a switch already returning the default value
Wow. Three good answers in less than 1/2 hour! I struggled for hours!
Thanks to you all!
Offline
Re: Problem with txp:php and a switch already returning the default value
azw a écrit:
Wow. Three good answers in less than 1/2 hour! I struggled for hours!
Trying for hours make you learn better ;)
Cheers
Offline
#7 2008-09-15 13:33:43
- azw
- Member
- Registered: 2007-01-29
- Posts: 279
Re: Problem with txp:php and a switch already returning the default value
And sleep less….
Offline