We help IT Professionals succeed at work.

php link rotating

dolythgoe asked
Hi all,

I'm looking to pick your expert brains on trying to achieve some functionality.

I have a web page which will get numurous page views (as they do!) and looking to serve some data (e.g. a link) from the database onto that page.

Now, there could be 3-5 bits of data in the db of which I want to display.

What I want to do is rotate through them so in 5 pageviews 5 bits of data will be displayed once.

I'm thinking perhaps there's a flag in the db which updates to say which one is to be displayed next and just updates in sequential order..

Any thoughts on this most welcome :)
Watch Question

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

You want to set a cookie with the current view and update it each time a User views the page so that each User gets all 5 views.  If you just do it on the server side, then one user could possible get the same view each time.  You would have no way of knowing.

Although on one page, I just use a random number in PHP to select the next image.  It's just a background image and not an ad.


I don't mind too much if the same person sees the same ad - more about getting a even spread of delivery for the ads.

A little concerned on the overhead of upating the db each time but they would be very light queries so perhaps not too much a worry. I've got memcache setup so it may be better to store the ads in there so it's just memory access?
Most Valuable Expert 2011
Top Expert 2016
Rotating ads would not be the same thing as random ads.  And you might want to decide whether you are going to have the ads appear in a consistent order during a site visit, restarting with the first ad on new site visits.  An even spread of the delivery of ads needs to be calculated and recorded.  Otherwise you will have bubbles in the ad stream.  Consider what happens when you flip a coin.  It can come up tails 5 times in a row!  So can a randomization scheme for ads.

If you want random advertisements, use ORDER BY RAND when you SELECT the ad from the data base.

If you want sequential advertisements on a per-visit basis, use in incrementing counter with modulo "n" where "n" is the count of ad rows in the data base.  Store the counter in the $_SESSION array.

If you want sequential advertisements on a per-user basis across several site visits, set (or reset) a cookie and use the incrementing modulo, storing the new value in the cookie or in the data base table associated with the cookie.

Do not worry about data base performance at all.  

If you want to do this the  semi-professional way, use Google Adsense.

If you want to do this the fully professional way, contact DoubleClick or UnderTone and serve their ads for pay.


Sorry for the late closing of this - back from hols!