437,830 Members | 2,276 Online Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

# How to rotate code based on how many visits already recorded to DB?

5 Replies

 Expert 5K+ P: 5,058 phpNerd01, please read your Private Messages. (Top of the page, under Network) Thank you. Sep 9 '08 #3

 Expert 5K+ P: 5,058 As to the problem. You could also try creating an algorithm that would show a random add based on the ratio you want, but adjust the ratio on-the-fly to reduce the chance that the actual ratio is way of the target ratio. That could be calculated like so: Expand|Select|Wrap|Line Numbers // Calculate a bias ratio. // This ratio will increase the odds that the next hit corrects // the current ratio. The further away from the target ratio the // current ration is, the more this will affect the next hit. \$biasRatio = \$targetRatio - (\$currentRatio - \$targetRatio);   // Get a random number between 0 and 1 \$random = mt_rand(0, 100) / 100;   // Check if the random number falls within the bias ratio. // If it does, show the primary, if not, show the secondary. if(\$random <= \$biasRatio) {     // Show primary } else {     // Show secondary }   You would have to calculate the \$currentRatio based on how many times each add has already been shown, and the \$targetRatio would be the percentage of times the primary add would be shown. This would probably not give you the exact ratio you were aiming at, but after like a 100 hits the ratio should balance out. The more hits the more accurate this would be. Sep 9 '08 #4

 P: 9 Hi Guys!!! Atli and PhpNerd, thank you very much for your assistance!!! I was in the process of moving homes and I just got my net re-connected again. Sorry I could not check the posts before!!! I will try to work out the above solutions and see what I get!!! Atli, the vars \$biasRatio and \$targetRatio have any values? Or \$targetRatio is the actual value from the DB?? How about \$biasRatio? Thanks!!! Sep 12 '08 #5

 Expert 5K+ P: 5,058 Atli, the vars \$biasRatio and \$targetRatio have any values? Or \$targetRatio is the actual value from the DB?? How about \$biasRatio? The target ratio would be the percentage of times you want your primary add shown (Like say; 0.8 for 80% of the time) The current ratio is the is the actual ratio, calculated based on how many times the primary add as actually been shown. (primary add shown / total adds shown) This info would have to be updated and calculated based on data stored in a database. The bias ratio is the modified target ratio, calculated based on how far away from the target ratio the current ratio is. This is done so that the final results are as close to the target ratio as possible, while still giving a *randomly generated* list of adds. Sep 12 '08 #6 