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
+27.8k Golang : How to create new XML file ?
+13.2k Golang : On enumeration
+6.8k Golang : Sort words with first uppercase letter
+11.6k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+10.7k Golang : Split strings into command line arguments
+8.3k Golang : Web(Javascript) to server-side websocket example
+5.6k Golang : Gargish-English language translator
+12.7k Golang : convert rune to unicode hexadecimal value and back to rune character
+7.3k Useful methods to access blocked websites
+4.4k PHP : Fix Call to undefined function curl_init() error
+2.2k Golang : Switch Redis database redis.NewClient
+37.5k Golang : Convert to io.ReadSeeker type