howster2011 Posted December 7, 2011 Share Posted December 7, 2011 Hi Guys, Newbie trying to understand PHP conditionals. In the below [[*id]] is a global variable which equates to my current page id so it could have the value of 1 for example. I tested it using echo and the correct values pass to it in the cms I'm using. What I cant achieve is for the conditionals below to work correctly so perhaps you can see a syntax error or show me away of doing the same using an array or something perhaps? THANKS <?php $id='[[*id]]' if ($id=="1" || $id=="2") echo "we are in 1 or 2"; elseif ($id=="3" || $id=="4") echo "we are in 3 or 4"; else echo "not in 1 to 4"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/ Share on other sites More sharing options...
Pikachu2000 Posted December 7, 2011 Share Posted December 7, 2011 Perhaps you should echo $id and see what value it actually holds . . . Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295425 Share on other sites More sharing options...
howster2011 Posted December 7, 2011 Author Share Posted December 7, 2011 Hi there, When I echo $id specifically it returns the correct value i.e. the page id. Thats tried and tested. The issue seems to be the syntax of the OR operators. I tried numerous variants of using =,==," and ' Thanks Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295431 Share on other sites More sharing options...
Pikachu2000 Posted December 7, 2011 Share Posted December 7, 2011 Then that isn't the actual code you're using, because $id should contain the string literal '[[*id]]', since it's in single quotes. You should also have a parse error if that's the actual code. Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295435 Share on other sites More sharing options...
howster2011 Posted December 7, 2011 Author Share Posted December 7, 2011 I tested using $id='[[*id]]'; - sorry missing ; - Still doesnt work unfortunately Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295444 Share on other sites More sharing options...
howster2011 Posted December 7, 2011 Author Share Posted December 7, 2011 if eg I am in page with id 3 the code evaluates as "we are in 1 or 2" as opposed to "we are in 3 or 4" If i am in page with id 1 the code evaluates "correctly" as "we are in 1 or 2" Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295448 Share on other sites More sharing options...
howster2011 Posted December 7, 2011 Author Share Posted December 7, 2011 stumbled across a solution. Using xor instead of || seems to work. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295449 Share on other sites More sharing options...
howster2011 Posted December 7, 2011 Author Share Posted December 7, 2011 spoke to soon xor appears to work for ($id="1" xor $id="2") i.e comparing 2 ids. If I want to compare 3 or 4 eg ($id="11" xor $id="12" xor $id="13" xor $id="14") it breaks again. So back to || it seems or some other OR type operator Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295457 Share on other sites More sharing options...
meltingpoint Posted December 7, 2011 Share Posted December 7, 2011 Maybe try switch() <?php switch($id) { case "1": echo "We are in 1 or 2"; break; case "2": echo "We are in 1 or 2"; break; case "3": echo "We are in 3 or 4"; break; case "4": echo "We are in 3 or 4"; break; default: echo "Not in 1 to 4"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295477 Share on other sites More sharing options...
howster2011 Posted December 7, 2011 Author Share Posted December 7, 2011 Thanks meltingpoint - I looked at switch but i need to incorporate ORs via if elseif else statements - its just way handier if I can get it to work. I assume I can use multiple elseif's if elseif elseif else Maybe there is some caching of the $id value going on or I have to wrap the $id in some other syntax. Hmmm Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295501 Share on other sites More sharing options...
meltingpoint Posted December 7, 2011 Share Posted December 7, 2011 <?php $id = '2'; if ($id=="1" || $id=="2") { echo "we are in 1 or 2"; } elseif ($id=="3" || $id=="4") { echo "we are in 3 or 4"; } else { echo "not in 1 to 4"; } ?> This works for me. If your $id is as it should be- then it should work. Have your tried var_dump($id) to make sure? Also, I have run across this before where I need to trim($id) and then do the comparison as in some instances a new line or space was there that was not making it evaluate correctly. Just a thought. Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295505 Share on other sites More sharing options...
howster2011 Posted December 7, 2011 Author Share Posted December 7, 2011 Hi, How would you integrate var_dump please? I think the issue is with $id holding the value of [[*id]] correctly. I am basing the expressions off this code (and yours) which works fine <?php $RandomNumber = rand(1, 4); if ($RandomNumber == '1') { // PHP If Statement echo 'The number was 1'; } elseif ($RandomNumber == '2') { // PHP Elseif Statement echo 'The number was 2'; } elseif ($RandomNumber == '3') { // PHP Elseif Statement echo 'The number was 3'; } else { echo 'The number was 4'; // PHP Else } ?> var_dump may work if you wouldnt mind showing me how to integrate it. Ultimarely I need this code below to work where i call html code based on page id <?php $resource = '[[*id]]'; if ($resource == '3' || $resource == '24' || $resource == '35' || $resource == '38' || $resource == '42' || $resource == '44' || $resource == '47' || $resource == '32'){ echo "[[!include? &file==`assets/templates/inc/newslisting.php]]"; <--this displays [[!include?&file==`assets/templates/inc/textContent.php]] incorrectly } else if ($resource == '54' || $resource == '64' || $resource == '75'){ echo "[[!include?&file==`assets/templates/inc/bloglisting.php]]"; <-- this displays blank i.e. echo fails } else if ($resource == '56'){ echo "[[!include?&file==`assets/templates/inc/contact.php]]"; <-- this display correctly } else{ echo "[[!include?&file==`assets/templates/inc/textContent.php]]"; <-- this displays correctly } ?> I said I'd query the concept in more basic terms above before tackling this specific code hence different names. The [[include...]] part evaluates to a chunk of html Thanks Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295511 Share on other sites More sharing options...
meltingpoint Posted December 7, 2011 Share Posted December 7, 2011 var_dump() is simply used to see exactly what your variable is. For example; <?php $a = ' a';//------blank space before the a $b = 'b';//------ b with no spaces $c = ' 1';//-------c with 3 spaces before it $d = array("a", "b", "c"); var_dump($a); echo "<br>"; var_dump($b); echo "<br>"; var_dump($c); echo "<br>"; var_dump($d); ?> This would out put : string(2) " a" string(1) "b" string(4) " 1" array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" } You would then be able to evaluate if the variable that you are comparing (in this case $id) is what you are expecting it to be and if the php parser is seeing it as such. Example above if you were expecting the variable $a to out put 'a' and you compare it to that- it would be false as it is actually a space and then a - see the out put is a string with (2) characters or in the case of $d - it is an array Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295513 Share on other sites More sharing options...
howster2011 Posted December 7, 2011 Author Share Posted December 7, 2011 ok thanks - I get string(7) "3" and string(7) "54" for the pages causing the issues and these ids are completely correct. The [[ syntax must be confusing things. Perhaps they should be wrapped in something other than ' or ". Any conditional with || is basically getting screwed up. Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295518 Share on other sites More sharing options...
kicken Posted December 7, 2011 Share Posted December 7, 2011 string(7) "3" and string(7) "54" See the (7) there? That means the string is 7 characters in length. It must be padded with spaces or newlines or some other character. Try using trim and see if that helps. Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295520 Share on other sites More sharing options...
meltingpoint Posted December 7, 2011 Share Posted December 7, 2011 in your code- under $resource = '[[*id]]'; place this; $resource = trim($resource); See what that does............... Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295524 Share on other sites More sharing options...
howster2011 Posted December 7, 2011 Author Share Posted December 7, 2011 Unfortunately it still shows eg string(7) "3" and there are 7 characters in [[*id]] - should I perhaps wrap [[*id]] in something other that single or double quotes? Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295525 Share on other sites More sharing options...
meltingpoint Posted December 7, 2011 Share Posted December 7, 2011 We would have to see exactly how you are obtaining the variable $id in order to see why it has 7 characters in it in stead of just one (in the case of page "3") and 7 characters in it in stead of just two (in the case of page "54") Can you post that code? Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295526 Share on other sites More sharing options...
howster2011 Posted December 7, 2011 Author Share Posted December 7, 2011 Its a modx system tag - the only light I can shed on it is: http://rtfm.modx.com/display/revolution20/Commonly+Used+Template+Tags - hopefully this helps Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295529 Share on other sites More sharing options...
meltingpoint Posted December 7, 2011 Share Posted December 7, 2011 Ok- what cms are you using? If like Joomla - then your link to the page would look like this; http://mywebsite.com/index.php?option=com_content&view=section&id=5&Itemid=57 Is that the type of link you are trying to extract the page number from? Nevermind- modx is the cms That said- I am unsure how you are accessing the page number as the $id. I do not work with Modx so I am of little help there. Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295530 Share on other sites More sharing options...
meltingpoint Posted December 7, 2011 Share Posted December 7, 2011 Can you provide a sample link from your site that shows how MODX 's url would look like? For privacy- simply put "mysite" in place of your real site. Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295532 Share on other sites More sharing options...
howster2011 Posted December 7, 2011 Author Share Posted December 7, 2011 Its modx i.e. www.modx.com and the id would look like mydomain.com/index.php?id=18 if furls are off. The specific "Resource ID" is the number of digits in a number i.e 100 is 3 digits, 50 is 2 and so on A string of 7 shouldnt come into play only thats its weird [[*id]] has 7 digits/characters Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295534 Share on other sites More sharing options...
howster2011 Posted December 7, 2011 Author Share Posted December 7, 2011 Will look at it again in the morning. Getting late here in Ireland. THANKS A MILLION for all your time and help meltingpoint. I think modx has a setting that makes this nigh impossible. Will start a thread on their forum tomorrow. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295541 Share on other sites More sharing options...
meltingpoint Posted December 8, 2011 Share Posted December 8, 2011 Ok- check back as I am working on a way to do this. Question- will your "ID" numbers be greater than 99? Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295546 Share on other sites More sharing options...
meltingpoint Posted December 8, 2011 Share Posted December 8, 2011 Tested this on my server and it works fine. There may well be a simpler solution, but until we determine why the MODX id is padding with spaces or \n, this is the only way I could think to do it. <?php $webpage = $_SERVER['SCRIPT_NAME'];//-----gets the page url as a string $find_string = 'id=';//-------defines our search starting point to find the position just before the Modx page ID number ex. id=18 //------*****THIS MUST MATCH EXACTLY HOW MODX SHOWS THE ID IN THE URL. If you need to- modify it in the line above $pos = strpos($webpage, $find_string);//---finds the character position where id= is found $v = substr($webpage, $pos+3 , 3);//----creates our id variable from the MODX url $v = trim($v); //---- id= is a string with 3 characters. So we add 3 to the located position thus winding up with the Modx page ID ex. 18 // this will extract any page id between 1 and 999. **This assumes MODX always puts the page id last in the url // //---------------------------------------------------------------------------------------------------- //------now lets create an easier way to accomplish your task of outputting html depdending on the page selected //----------------OR, if you are more comfortable you can implement your if and elseif statements as you had them // $resource1 = array("3", "24", "32", "35", "38", "42", "44", "47");//---------define our array for certain group of pages if(in_array($v, $resource1))//--------if the page id matches this group, echo the html { echo "[[!include? &file==`assets/templates/inc/newslisting.php]]"; exit; } // $resource2 = array("54", "64", "75");//---------define our array for another set of pages if(in_array($v, $resource2))//--------if the page id matches this group, echo the html { echo "[[!include?&file==`assets/templates/inc/bloglisting.php]]"; exit; } // $resource3 = array("56"); if(in_array($v, $resource3))//--------if the page id matches this group, echo the html { echo "[[!include?&file==`assets/templates/inc/contact.php]]"; exit; } // $merged = array_merge($resource1, $resource2, $resource3);//--- HERE WE CREATE THE DEFAULT. IF THE ID ISN'T IN ANY OF THE ABOVE if(!in_array($v, $merged)) { echo "[[!include?&file==`assets/templates/inc/textContent.php]]"; exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/#findComment-1295581 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.