NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page is this the best way ?

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

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 05-14-2007, 08:18 AM THREAD STARTER               #1 (permalink)
Senior Member
 
champ_rock's Avatar
Join Date: Oct 2006
Location: http://akshayjain.org
Posts: 2,830
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?
champ_rock is offline  
Old 05-14-2007, 09:00 AM   #2 (permalink)
JFS
NamePros Regular


 
JFS's Avatar
Join Date: Oct 2005
Location: Portugal
Posts: 800
JFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to all
 



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
????: NamePros.com http://www.namepros.com/programming/327686-is-this-the-best-way.html

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
JFS is online now  
Old 05-14-2007, 01:11 PM   #3 (permalink)
cef
NamePros Regular
Join Date: May 2004
Location: NYC
Posts: 236
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 1;
????: NamePros.com http://www.namepros.com/showthread.php?t=327686

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); 
????: NamePros.com http://www.namepros.com/showthread.php?t=327686
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


Liquid Web Smart Servers  
All times are GMT -7. The time now is 03:47 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger