I need to make a banner rotator on the site. That is, the client puts 3 banners in one place and says: 1st - 20%, 2nd - 30%, 3rd - 50%. It is clear that the third banner will most often be displayed. Just how to calculate it correctly? Maybe there is a ready-made plug-in?

If who to do - share reasons where to dig

    1 answer 1

    In short, the algorithm may look like this.

    $b[1] = ['rate' => 20, 'title' => 'title1', 'url' => 'url1']; $b[2] = ['rate' => 30, 'title' => 'title2', 'url' => 'url2']; $b[3] = ['rate' => 50, 'title' => 'title3', 'url' => 'url3']; $show = $b[1]; $rand = random_int(1, 100); foreach($b as $k => $v){ if($rand >= $v['rate']){ continue; } $show = $b[$k]; }