<?php $banners = array( '' => '', '' => '', '' => '', ); foreach ($banners as $domain => $banner) { if(stristr($_SERVER['SERVER_NAME'], $domain)) { echo $banner; } } ?> Code (markup):
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.
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.
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.
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.
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.
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.
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: