Php OR Question

Hi,

I’m trying to display a banner but it needs to change to code a little. I’m attempting to use the OR i.e. || like below but it doesn’t work. It works fine without it but I need it for two pages. How do I do this?


<?php $host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == 'www.mysitexx.com/' || 'www.mysitexx.com/?___store=spanish&___from_store=english'){
echo $this->__('<div class="col-left-banner sidebar">'); }
else {
echo $this->__('<div class="col-left sidebar">'); }
?>

Thanks

You need to remember that || and && allow two conditions to be considered, and as such, you need to provide a full condition on each side of the operator.

if($host == 'www.mysitexx.com/' || $host == 'www.mysitexx.com/?___store=spanish&___from_store=english'){

Thanks, I’ll remember that as I’ve play with these && and || for hours only sometimes to luck up. :slight_smile:

Hi,

I’m still working on this some.

Here’s my code:


                        <?php $host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
                        if($host == 'www.mysiteoo.com/' ||
						$host == 'www.mysiteoo.com/es/'){
                        echo $this->__('<div class="col-left-banner sidebar">'); }
                        else {
                        echo $this->__('<div class="col-left sidebar">'); }
                        ?>

My problem is I’ve removed the &___store=en&___from_store=es from the url witch is good.

It goes like this:

www.mysiteoo.com/ (switch to)
www.mysiteoo.com/es/ (switch back)
http://www.mysiteoo.com/?SID=3qaveoevuc2uq3n64tqbri2495&___store=en&___from_store=es

See it puts the ?SID=(Different numbers here)

How can I write my code to use the different <div> when the SID=Numbers will be different all the time?


$sid = $_GET['SID'];
if ($sid == 'whatever') {
    // Do something
}

Thanks

I don’t understand what I should put into “whatever” because the sid changes. What would I add there?

I think you will want to use strpos() for this to see if the $host contains this domain and folder path.

<?php 
  $host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; 

  if(strpos(strtolower($host), 'www.mysiteoo.com/') === true 
    || strpos(strtolower($host), 'www.mysiteoo.com/es/') === true)
  { 
    echo $this->__('<div class="col-left-banner sidebar">'); 
  } else { 
    echo $this->__('<div class="col-left sidebar">'); 
  }  
?>

I tried this but it didn’t work. I’m not sure if you know what I trying to do so I made this little image so you can see.

I would like to add a banner to this page so I have to use a different <div> with a negative top margin.

When do you want to use each div, if I know that, I can help you accomplish it.

[edit]Bah! I made a mistake in my initial code (probably due to my lack of caffeine)

<?php
  $host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

  if(strpos(strtolower($host), 'www.mysiteoo.com/') !== false
    || strpos(strtolower($host), 'www.mysiteoo.com/es/') !== false)
  {
    echo $this->__('<div class="col-left-banner sidebar">');
  } else {
    echo $this->__('<div class="col-left sidebar">');
  }
?>

It should be !== false instead of === true[/edit]

It works! I don’t know how but it works.

I’m glad your getting your caffeine.

Thanks

:smiley:

Let me explain how it works, so at least you know :slight_smile:

<?php  
  $host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];  

  if(strpos(strtolower($host), 'www.mysiteoo.com/') !== false  
    || strpos(strtolower($host), 'www.mysiteoo.com/es/') !== false) 
  {  
    echo $this->__('<div class="col-left-banner sidebar">');  
  } else {  
    echo $this->__('<div class="col-left sidebar">');  
  }   
?>

Okay, so you know how the $host variable is being generated (as that was your original code)

[fphp]strpos[/fphp] is a neat function, think of it like indexOf in other languages (.NET, Java, etc). In short, it takes your input ($host), and looks for a string stored within it (‘www.mysiteoo.com/’). Since the string we are looking for is all lowercase, I forced the $host variable to be lowercase too (I believe there is a stripos function, that is case-insensitive, but I forgot to look it up (just looked it up, and there is, so you could use that instead of doing the strtolower()).

So you could use (assuming you are on PHP 5):

<?php  
  $host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];  

  if(stripos($host, 'www.mysiteoo.com/') !== false  
    || stripos($host, 'www.mysiteoo.com/es/') !== false) 
  {  
    echo $this->__('<div class="col-left-banner sidebar">');  
  } else {  
    echo $this->__('<div class="col-left sidebar">');  
  }   
?>

Anyway, the important thing about strpos and stripos is that you can get a value of 0 and it is VALID! It simply means the string you were searching for was found at the beginning of the input. Otherwise, it will return the index of where that string started/was found.

Consider the following examples:

$host = 'http://www.mysiteoo.com/';
var_dump(stripos($host, 'www.mysiteoo.com/')); // should show 7

$host = 'www.mysiteoo.com/';
var_dump(stripos($host, 'www.mysiteoo.com/')); // should show 0

If you simply checked if (strpos($host, ‘www.mysiteoo.com/’)), and $host was equal to ‘www.mysiteoo.com/’, the IF statement would return FALSE because 0 is FALSE. So you have to use a strict condition !== false so it accepts 0 as being a result for TRUE.

Hope this helps, and if any of it needs further explanation, just let me know.

Very good explanation. Thanks