PHP : Shuffle to display different content or advertisement
Problem :
Your website serves advertisement and you are looking for ways to display different images or content each time a page is loaded.
Solution :
- Create an array.
- Load the array with different HTML codes.
- Shuffle the array content and then pick the item 0.
For example :
<div class="shuffled-content">
<?php
$ads = array();
$ads[] = '<a href="http://www.edu.joshuatly.com/?utm_source=HomeTution&utm_medium=SideBanner&utm_campaign=HomeTutionBanner" rel="nofollow" target="_blank"><img style="cursor: pointer" src="https://www.hometuitionjob.com/public/images/ads/edujoshuatly250x250.gif"></a>';
$ads[] = '<a href="http://www.guru-app.com/?utm_source=HomeTution&utm_medium=SideBanner&utm_campaign=HomeTutionBanner" rel="nofollow" target="_blank"><img style="cursor: pointer" src="https://www.hometuitionjob.com/public/images/ads/guru-app250x250.jpg"></a>';
shuffle($ads);
echo $ads[0];
?>
<span style="font-weight: bold;cursor: pointer;">Advertise here? Email ads@example-ads.com</span>
</div>
In this way, your website will display the ads on random order each time a page is loaded or refreshed.
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.4k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+22.2k Golang : Use TLS version 1.2 and enforce server security configuration over client
+6.5k Golang : Detect face in uploaded photo like GPlus
+7.9k Gogland : Where to put source code files in package directory for rookie
+18.7k Golang : Aligning strings to right, left and center with fill example
+9.7k Golang : Validate IPv6 example
+20.9k Golang : Secure(TLS) connection between server and client
+14.8k How to automatically restart your crashed Golang server
+6.2k PHP : How to check if an array is empty ?
+15.1k Golang : Submit web forms without browser by http.PostForm example
+8.5k Prevent Write failed: Broken pipe problem during ssh session with screen command
+7k Golang : Get expvar(export variables) to work with multiplexer