hi,
i want to set a conditinal in php for a global variable.
here some code to explain it
$cat=2;
echo "CAT:".$cat."<br>";
$curcat_id="{current_edition_catid}";
echo "VAR:".$curcat_id."<br>";
$curcat_id=trim($curcat_id);
echo "TRIM:".$curcat_id."<br>";
if ($cat==$curcat_id) {
echo "two is two <br>";
} else {
echo "two is not two <br>";
}
result is CAT:2 VAR:2 TRIM:2 two is not two
if i try this i get SETTYPE 1
$curcatset_id=settype($curcat_id, "integer");
echo "SETTYPE ".$curcatset_id;
it’s really important for me to get it working, this actually already aworkaround because i can’t use categories in reverse_related_entries…
Hi davedriesmans,
Have you tried looking for add-ons to help you achieve what your looking to do? There are many add-ons devoted to working with catgories - check here.
This is really a php question so I’m going to move this thread to programming and development. Feel free to post again as needed.
Sean
if i try this i get SETTYPE 1$curcatset_id=settype($curcat_id, "integer"); echo "SETTYPE ".$curcatset_id;
settype() returns a bool for success/failure. That would be why $curcatset_id returns 1.
You could try:
$curcat_id=trim("{current_edition_catid}");
echo gettype($curcat_id);
settype($curcat_id, "integer");
echo gettype($curcat_id);
But a better way would be to use PHP’s intval() function:
$curcat_id=intval(trim("{current_edition_catid}"));
But… it would be better to do things in “native” EE if possible (using a plugin or alternative method).
hi ian, sean,
ian, you’re right about settype() off course
i developed it all with php and i can’t see an add on that would do the trick. i developed it all, except that global variable that is doing weird….
my global variable has the value 2 but it’s something weird with EEs global variable
the intval doesn’t want to work:
orginal var: 2 gettype: string trim:2 intval:0
the settype integer doesn’t want to work as well: orginal var:2 gettype: string trim:2 settype worked:1 settype new value:0
if i put a $curcat_id=”2”; in the beginning instead of $curcat_id=”{current_edition_catid}”; i get orginal var:2 gettype: string trim:2 settype worked:1 settype new value:2
so it seems the EE global variable is more then just the variable…???
dave
so it seems the EE global variable is more then just the variable…???
When do you have PHP processing, is it at the start or finish? (This is a template option).
If it is at the start then it would be before EE has processed any tags (iirc) and so it would be trying to get the value of the string “{current_edition_catid}”
hi ian, PHP Parsing Stage is at input it does give me the correct value (2) but for some reason it’s not a real string as i can’t convert it to an integer or put it in a conditional… dave
Ahhh, no, it doesn’t give you the correct value. What it is doing is setting the value to the string, which is then processed by EE in the output so it looks like it was set correctly.
Try this:
echo "-".substr("{current_edition_catid}", 1, strlen("current_edition_catid") - 2)."-";
This will basically strip off the {} and replace them with -’s, so EE won’t parse the variable. That will tell you if EE is parsing it before or after the PHP runs.
I expect you will find it will be parsed after the PHP has run, and you’ll be left with a string: -current_edition_catid-
oh, i see, genius! 😊 and i guess there is no way to force EE to parse it before the php?
Well if you set PHP to be parsed on output, then all the EE tags will have been parsed before the PHP.
It depends on the interaction of your PHP and your EE tags. In this case your PHP needs to act on the EE tags (so after everything, or on output), but others cases might need the EE tags to be changed based on some PHP (so before EE, or on input).
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.