SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Php OR Question

  1. #1
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 Code:
    <?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

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,138
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    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.

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

  3. #3
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'll remember that as I've play with these && and || for hours only sometimes to luck up.

  4. #4
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I'm still working on this some.

    Here's my code:
    PHP 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=3qaveoe..._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?

  5. #5
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sid $_GET['SID'];
    if (
    $sid == 'whatever') {
        
    // Do something

    Keith
    Freelance web developer
    http://www.duvalltech.com/

  6. #6
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kduv View Post
    PHP Code:
    $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?

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,138
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    I think you will want to use strpos() for this to see if the $host contains this domain and folder path.
    PHP Code:
    <?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">'); 
      }  
    ?>

  8. #8
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    I think you will want to use strpos() for this to see if the $host contains this domain and folder path.
    PHP Code:
    <?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.banner.jpg

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

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,138
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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

  10. #10
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    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 Code:
    <?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
    It works! I don't know how but it works.

    I'm glad your getting your caffeine.

    Thanks

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,138
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Freejoy View Post
    It works! I don't know how but it works.

    I'm glad your getting your caffeine.

    Thanks


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

    PHP Code:
    <?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)

    strpos 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 Code:
    <?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:
    PHP Code:
    $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.

  12. #12
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very good explanation. Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •