Why the following code doesnt work?

Discussion in 'PHP' started by joliett89, May 13, 2013.

  1. #1
    <?php
    $banners = array(
    '' => '',
    '' => '',
    '' => '',
    );
    foreach ($banners as $domain => $banner) {
    if(stristr($_SERVER['SERVER_NAME'], $domain)) {
    echo $banner;
    }
    }
    ?>
    Code (markup):

     
    Last edited by a moderator: May 13, 2013
    joliett89, May 13, 2013 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Please fill the array with example data. It's impossible to tell without proper data.

    Or echo $_SERVER['SERVER_NAME'] and $domain,... see what they contain. This should be fairly easy to debug.
     
    nico_swd, May 13, 2013 IP
  3. joliett89

    joliett89 Active Member

    Messages:
    124
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    It can be something like this:
    <?php
    $banners = array(
    'domain1.com' => '<a href="name1.com"><img src="image1.jpg></a>',
    'domain2.com' => '<a href="name2.com"><img src="image2.jpg></a>',
    'domain3.com' => '<a href="name3.com"><img src="image3.jpg></a>',
    );
    foreach ($banners as $domain => $banner) {
    if(stristr($_SERVER['SERVER_NAME'], $domain)) {
    echo $banner;
    }
    }
    ?>
    I get some errors with online php checking tools, but I cant figure out whats wrong. One of them says that there is a missing delimiter, another something else. I use a code that is similar somewhere else, and it works fine.
     
    joliett89, May 13, 2013 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    The syntax is file, and it's working on my machine. You do realize that $_SERVER['SERVER_NAME'] contains the name of the server the script is currently running on, right? So unless you have multiple domains for your site, or you distribute the script to others, this won't make much sense.​
     
    nico_swd, May 13, 2013 IP
  5. joliett89

    joliett89 Active Member

    Messages:
    124
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    The code is located in sidebar.php of a Wordpress blog. It is a multisite installation, with over 100 domains. It is suppose to display a banner, depending on a domain name. I have similar script, for different set of banners, and it works.
     
    joliett89, May 13, 2013 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Okay, that makes sense. Put this in the foreach() loop, run the script again, and see what it outputs.
    
    echo '<!-- ', $_SERVER['SERVER_NAME'], ' : ', $domain, ' -->';
    
    PHP:
    (Since it's a comment, it's only visible in the source code).

    On a site note, you should add a break; after the echo. Since you have over 100 domains, and perhaps that many banners, the loop would run over a 100 times each time. The break prevents that, and you save some processing time and resources.
     
    nico_swd, May 13, 2013 IP
  7. joliett89

    joliett89 Active Member

    Messages:
    124
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    For some reason, I can't get it to work. I have a folder with banners, and they need to be displayed on a website, depending on a domain name. Is there any way somebody could suggest me the code? Thank you.
     
    joliett89, May 13, 2013 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    The code above does work. In order to tell why it doesn't work on your server, I need to debug it. If you could add the line I posted above to your code, then run it, and tell me the output, that would be quite helpful.

    Also, I'd probably do it this way.
    
    $banners = array(
        'domain1.com' => '<a href="name1.com"><img src="image1.jpg></a>',
        'domain2.com' => '<a href="name2.com"><img src="image2.jpg></a>',
        'domain3.com' => '<a href="name3.com"><img src="image3.jpg></a>',
    );
     
    $server = str_replace('www.', '', $_SERVER['SERVER_NAME']);
     
    if (isset($banners[$server]))
    {
        echo $banners[$server];
    }
    
    PHP:
     
    nico_swd, May 14, 2013 IP