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
+12k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+16.5k Golang : Send email and SMTP configuration example
+6.1k Fontello : How to load and use fonts?
+5.5k Golang *File points to a file or directory ?
+36.4k Golang : Convert(cast) int64 to string
+7.2k Golang : Transform lisp or spinal case to Pascal case example
+17k Golang : Set up source IP address before making HTTP request
+16.7k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+8.7k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+42k Golang : How do I convert int to uint8?
+10k Golang : Get escape characters \u form from unicode characters
+9.5k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases