Jump to content

if ,else and else if statements using OR


howster2011

Recommended Posts

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";
?>

 

Link to comment
https://forums.phpfreaks.com/topic/252697-if-else-and-else-if-statements-using-or/
Share on other sites

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.

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";
}
?>

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

<?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.

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

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

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.

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?

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.

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

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;
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.