We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

global variables & integers in php

Development and Programming

davedriesmans's avatar
davedriesmans
14 posts
14 years ago
davedriesmans's avatar davedriesmans

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…

       
Sean C. Smith's avatar
Sean C. Smith
3,818 posts
14 years ago
Sean C. Smith's avatar Sean C. Smith

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

       
Ian from Aus's avatar
Ian from Aus
94 posts
14 years ago
Ian from Aus's avatar Ian from Aus
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).

       
davedriesmans's avatar
davedriesmans
14 posts
14 years ago
davedriesmans's avatar davedriesmans

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

       
Ian from Aus's avatar
Ian from Aus
94 posts
14 years ago
Ian from Aus's avatar Ian from Aus
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}”

       
davedriesmans's avatar
davedriesmans
14 posts
14 years ago
davedriesmans's avatar davedriesmans

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

       
Ian from Aus's avatar
Ian from Aus
94 posts
14 years ago
Ian from Aus's avatar Ian from Aus
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-

       
davedriesmans's avatar
davedriesmans
14 posts
14 years ago
davedriesmans's avatar davedriesmans

oh, i see, genius! 😊 and i guess there is no way to force EE to parse it before the php?

       
Ian from Aus's avatar
Ian from Aus
94 posts
14 years ago
Ian from Aus's avatar Ian from Aus
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).

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.