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
+4k Golang : Accessing dataframe-go element by row, column and name example
+17.5k Golang : Set and Get HTTP request headers example
+4.8k Prevent Write failed: Broken pipe problem during ssh session with screen command
+8.3k Golang : Post data with url.Values{}
+12.2k Golang : Multi threading or run two processes or more example
+4.7k Golang : Gonum standard normal random numbers example
+33.1k Golang : Get hardware information such as disk, memory and CPU usage
+11.1k Golang : Test floating point numbers not-a-number and infinite example
+2.9k Golang : Detect words using using consecutive letters in a given string
+7.1k Golang : How to protect your source code from client, hosting company or hacker?
+19.9k Get file path of temporary file in Go
+3.2k Golang : Join lines with certain suffix symbol example