This question already has an answer here:
here the sample case..
i want to display banner randomly by percentage based on visitor hits. for example i want to display ads 70% of visitor hits.. the problem is we don't know how many visitor is.
if it make easier we have set percentage 10%, 20%, 30% ... 100%
Thanks in advanced.
</div>
You don't need to know about how many visitors. The only thing you need to do is take a random number between 1 and 100, and if it is 70 or lower, it is within this 70% range.
Eventually this will work out, and display the banner to 70% of the people:
if (rand(1,100) <= 70) {
display_banner();
}
If you want to keep this number, and show it to the user for all page views, then store it in a $_SESSION var of some sort, and based on that value display the banner.
I believe
$percentageVisitors = ceil(($currentVisitors / $totalVisitors) * 100) ;
Solution
if( $percentageVisitors >= 70)
{
showRandomAdvert();
}
I hope this helps
Than
almost exactly what I do for banners as well, random sampling.
$freq_banners = array(
5 => 'banner_3',
10 => 'banner_1',
85 => 'banner_2',
);
$use_banner = null;
$sum = 0;
$key = rand(1,100);
foreach ( $freq_banners as $banner_freq => $banner ) {
$sum += $banner_freq;
if ( $key <= $sum ) {
$use_banner = $banner;
break;
}
}
Cheers