Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8960049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:35:08+00:00 2026-06-15T15:35:08+00:00

I’m working on building an ad banner rotation script based on impressions that displays

  • 0

I’m working on building an ad banner rotation script based on impressions that displays ads evenly throughout the month. The calculations will be done each time the ad is requested to be displayed. So this will be done on the fly. The ads should appear to rotate through, one after another, and not just display one ad for 1000 impressions, then the other ad for 1000 impressions. It for the most part should display for 1 impression, then switch ads (unless of course one ad has a lot more impressions than the other to use up).

Let’s say I have 5 ads and each has a different number of impressions that were purchased, what would be the formula/how do you serve up the ads? I’m looking to do this in PHP.

Ad #1: 1,000 purchased impressions

Ad #2: 12,000 purchased impressions

Ad #3: 3,000 purchased impressions

Ad #4: 20,000 purchased impressions

Ad #5: 10,000 purchased impressions

if there are multiple ads that bought 1000 impressions all for the same time frame, it should show one after another until the impressions are used. Though, I think it might be good that if a person bought 1000 impressions for a short time frame, I should account for that and show them at a faster rate. I’m open to suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:35:09+00:00Added an answer on June 15, 2026 at 3:35 pm

    I think you should use the best type of algorithm for the best JOB i’ll only show you some few possibility on how to implement such

    My current example would show using

    • shuffle
    • fisherYatesShuffle
    • robinShuffle
    • ratioShuffle

    You can also implement

    • Priory Based shuffle
    • Time Base Shuffle
    • Percentage
    • Click Shuffle
    • etc

    Simple Prove of Concept

    // Create Add Infroamtion
    $ads = array();
    $ads[] = new Ad(10, "A.jpg", 2);
    $ads[] = new Ad(12, "B.gif", 3);
    $ads[] = new Ad(30, "C.png", 7);
    $ads[] = new Ad(20, "D.swf", 5);
    
    // Add ads to banner
    $banner = new Banner($ads);
    
    // You can also add addional ads
    $banner->add(new Ad(10, "E.swf"));
    
    echo "<pre>";
    
    //Lets Emulate first 100 rotations 
    for($i = 0; $i < 1000; $i ++) {
        // Select Algorithm
        $banner->randomise("ratioShuffle");
    
        // Display Add
        echo $banner->getDisplay(), PHP_EOL;
    }
    

    Simple Shuffle Function that can be used

    function fisherYatesShuffle(array &$items) {
        for($i = count($items) - 1; $i > 0; $i --) {
            $j = @mt_rand(0, $i);
            $tmp = $items[$i];
            $items[$i] = $items[$j];
            $items[$j] = $tmp;
        }
    }
    
    function robinShuffle(array &$items) {
        usort($items, function ($a, $b) {
            $a = $a->getDisplay();
            $b = $b->getDisplay();
            return $a == $b ? 0 : ($a < $b ? - 1 : 1);
        });
    }
    
    function ratioShuffle(array &$items) {
        static $called = false;
        if ($called === false) {
            $ads = array();
            foreach ( $items as &$ad ) {
                for($i = 0; $i < $ad->getRatio(); $i ++) {
                    $ads[] = $ad;
                }
            }
            $called = true;
            $items = $ads;
        }
        shuffle($items);
    }
    

    Classes Used

    class Ad implements JsonSerializable {
        private $impressions;
        private $media;
        private $ratio = 1;
        private $display = 0;
    
        function __construct($impressions, $media = null, $ratio = 1) {
            $this->impressions = $impressions;
            $this->media = $media;
            $this->ratio = $ratio;
        }
    
        function torch() {
            $this->impressions --;
            $this->display ++;
        }
    
        public function getImpression() {
            return $this->impressions;
        }
    
        public function getDisplay() {
            return $this->display;
        }
    
        public function getRatio() {
            return $this->ratio;
        }
    
        public function getMeadia() {
            return $this->media;
        }
    
        public function __toString() {
            return json_encode($this->jsonSerialize());
        }
    
        public function jsonSerialize() {
            return get_object_vars($this);
        }
    }
    
    
    class Banner implements Countable, JsonSerializable {
        private $totalImpressions;
        private $ads = array();
    
        function __construct(array $ads) {
            foreach ( $ads as $ad )
                $this->add($ad);
        }
    
        public function add(Ad $ad) {
            $this->ads[] = $ad;
            $this->totalImpressions += $ad->getImpression();
        }
    
        public function randomise($function = null) {
            if (is_callable($function, false, $callable_name)) {
                return $callable_name($this->ads);
            } else {
                return shuffle($this->ads);
            }
        }
    
        public function getDisplay() {
            foreach ( $this->ads as &$ad ) {
                if ($ad->getImpression() < 1) {
                    unset($ad);
                    continue;
                }
                $ad->torch();
                break;
            }
            return isset($ad) ? $ad : null;
        }
    
        public function jsonSerialize() {
            $array = $this->ads;
            foreach ( $array as &$ad ) {
                $ad = $ad->jsonSerialize();
            }
            return $array;
        }
    
        public function __toString() {
            return json_encode($this->jsonSerialize());
        }
    
        function count() {
            return count($this->ads);
        }
    }
    

    As you can see this is an example …. Just try and make your solution flexible

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.