[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 05-14-2007, 07:18 AM   #1 (permalink)
Senior Member
 
champ_rock's Avatar
 
Join Date: Oct 2006
Location: http://akshayjain.org
Posts: 2,768
8,037.05 NP$ (Donate)

champ_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond repute


is this the best way ?

hi

i am trying to create a simple adsense rotation script which would display the ads in a certain ratio

if i have to distribute the ads in 50-50% ratio then i would follow this
Code:
<?php 
$rand = rand(1,2);
if ($rand == 1) 
print 'adsense code 1';

elseif ($rand == 2) 
print 'adsense code 2;
?>
but the problem is suppose i want to distribute the ads into a certain ratio of say 70-30%
then should i do it manually by using
Code:
rand(1,2,3,4,5,6,7,8,9,0)
and then using if statement to print out "code 1" 7 times and "code 2" 3 times?

or is there any better and more efficient way to do this all?
__________________
Akshay Jain
......................
champ_rock is offline  
Old 05-14-2007, 08:00 AM   #2 (permalink)
JFS
NamePros Regular
 
JFS's Avatar
 
Join Date: Oct 2005
Location: Portugal
Posts: 760
56.85 NP$ (Donate)

JFS is just really niceJFS is just really niceJFS is just really niceJFS is just really niceJFS is just really niceJFS is just really nice


Quote:
Originally Posted by champ_rock
Code:
<?php 
$rand = rand(1,2);
if ($rand == 1) 
print 'adsense code 1';

elseif ($rand == 2) 
print 'adsense code 2;
?>
a more eficient way would be

Code:
$adsense['1']= "code here";
$adsense['2']= "code here";
$rand = rand(1,2);


echo $adsense[$rand];
if you use rand(1,9) it gives you a random number between 1 and 9.
if you want percentages and want them precise you should use a database.
__________________
Joćo Fernandes Silva
Selling :
19P.ORG - ARCADEHITS.ORG - AZIAN.NET - COREFANS.COM - CTUTORIALS.NET - DEDISEEK.COM - HITCHECK.COM - HOST15.COM - HOSTCUSTOMER.COM - LARGETIPS.COM - SCRIPTCANDY.COM - VISUALBOOK.NET - VOXVPS.COM / .NET - WALLPAPERSARENA.COM
JFS is offline  
Old 05-14-2007, 12:11 PM   #3 (permalink)
cef
NamePros Regular
 
Join Date: May 2004
Location: NYC
Posts: 236
76.50 NP$ (Donate)

cef is a jewel in the roughcef is a jewel in the roughcef is a jewel in the rough

Animal Rescue
Here's a function that returns 0 or 1 based on which side of a user-definable threshold the random number ends up on. Explanation after the code:

PHP Code:
function rand_threshold($threshold)
{
     return
mt_rand(1,100) <= $threshold ? 0 : 1;
}
The function returns 0 if the random value is less than or equal to the threshold passed in as a parameter, or 1 if it's greater.

So...if you call the function as such:

PHP Code:
$rand = rand_threshold(30);
$rand will be 0 30% of the time, and 1 70% of the time. You can use the returned values as indexes into the array of ad codes.

If you want the opposite to happen, e.g. the SECOND ad code to come up only 30% of the time, just invert the threshold:

PHP Code:
$rand = rand_threshold(70);
For the 50/50 scenario you first outlined, it should be obvious what to do

PHP Code:
$rand = rand_threshold(50);
cef is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 01:10 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85