title: Advertising rotation: What's the best to determine when and what ad should be displayed on the page?
body: <p>A client I'm creating a site for wants a custom advertising "engine". The plan is to have a few ads on the site and fill the rest with Google Adsense until all the spots are full.</p>
  
  <p>My problem is how to determine which ad to dipslay. (Assume for now that I only have 1 ad placement.) My thinking was I'd have a table with:</p>
  
  <ul>
  <li>year</li>
  <li>month</li>
  <li>impressions for the month (0 for unlimited)</li>
  <li>used impressesion</li>
  <li>clients</li>
  <li>HTML code to display ad</li>
  </ul>
  
  <p>I could do something like to get the ads:</p>
  
  <pre><code>SELECT * 
  FROM ad 
  WHERE impressions &gt; used_impressions 
      OR impressesions = 0 
  ORDER BY RAND() 
  LIMIT 1
  </code></pre>
  
  <p>But, say I have 3 ads:</p>
  
  <ul>
  <li>1 ad -- 5000 impressions</li>
  <li>1 ad -- 5000 impressions</li>
  <li>Google Adsense filling the reaminder of the sites hits</li>
  </ul>
  
  <p>Statistically speaking all 3 ads would be displayed an equal number of times. By the end of the first week and 15000 hits on the site, the first 2 ads would have used all of their impressions and the remaining 3+ weeks of the month and not be displayed again; only Google Adsense would be displayed.</p>
  
  <p>How do I space out the ads so they are spread out over the month?</p>
  
  <p>I am using LAMP.</p>