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 [email protected]</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
+30.1k Golang : Generate random string
+13.1k Golang : error parsing regexp: invalid or unsupported Perl syntax
+7.3k Golang : Handling Yes No Quit query input
+8.5k Golang : Heap sort example
+14k Golang : How to pass map to html template and access the map's elements
+10.3k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example
+9.9k Golang : Use regular expression to get all upper case or lower case characters example
+7.9k Golang : Add build version and other information in executables
+7k Golang : Calculate how many weeks left to go in a given year
+5k Golang : Calculate half life decay example
+14.1k Golang : Send email with attachment(RFC2822) using Gmail API example
+21.2k Golang : GORM create record or insert new record into database example