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
+15.3k Golang : Get checkbox or extract multipart form data value example
+15k Golang : Delete certain files in a directory
+17.1k Golang : Get future or past hours, minutes or seconds
+17.2k Golang : [json: cannot unmarshal object into Go value of type]
+9.5k PHP : Get coordinates latitude/longitude from string
+11.4k How to tell if a binary(executable) file or web application is built with Golang?
+5.7k PHP : How to check if an array is empty ?
+6.9k Golang : Gorrila mux.Vars() function example
+4.8k Golang : Constant and variable names in native language
+20.7k Golang : For loop continue,break and range
+7.1k Golang : Individual and total number of words counter example
+22.9k Golang : Print out struct values in string format