Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 04-10-2005, 04:57 PM Random affiliates
lajkonik86's Avatar
Ultra Talker

Posts: 389
Trades: 0
Random affiliates in a mysql database with a table field for their importance.
how would i go about doing something random, and giving priority to more importants affiliates?


Already know how to properly generate a random number
PHP Code:
rand()&7
But yeay the part after that is a bit fuzzy
Problem is that i don't know enough about mysql and php in order to judge how to do this effectivly.
There's not really any way to store this in a cache and since it will be loaded loads of times sort of need to do this effective
__________________
Know what to Download

Please login or register to view this content. Registration is FREE

Last edited by lajkonik86; 04-10-2005 at 04:59 PM..
lajkonik86 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-10-2005, 06:37 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
You need some way of defining a numeric range for each affiliate, say give the first one 1 to 10, the next one gets 11 to 20, then the third one gets 21 to 40 cos he paid more. Then you can find the biggest number out of all the ranges, and generate a random number up to that value. Then you can grab the MySQL record that got picked.

So you'd need two columns, one called say 'lower' and the other called 'upper'. When you add an affiliate, you give them a certain number range by adding on to the last one. The top value of all the ranges can be found with MySQL too:

PHP Code:

//To add an affiliate

function addAffiliate($rangewidth,$name,$othervariables) {
  
$result mysql_query("SELECT upper FROM affiliates ORDER BY upper DESC LIMIT 1");
  
$current_max mysql_result($result,0,'upper');
  
$newlower $current_max 1;
  
$newupper $current_max $rangewidth;
  
mysql_query("INSERT INTO affiliates VALUES ( '$name' , $newlower$newupper$othervariables)");

Then to pick a random affiliate, you generate your random number. What you have above will generate one in the range 0 to 7 since you are using bitwise AND. I would write rand(1,n) to get a number from 1 to n:

PHP Code:
//To pick an affiliate

function pickAffiliate() {
  
$result mysql_query("SELECT upper FROM affiliates ORDER BY upper DESC LIMIT 1");
  
$current_max mysql_result($result,0,'upper');
  
$random rand(1,$current_max);
  
$result mysql_query("SELECT * FROM affiliates WHERE lower < $random ORDER BY lower DESC LIMIT 1");
  
$affiliate_array mysql_fetch_array($result);
  return 
$affiliate_array;

You then need to watch out if you change the range for one affiliate (like they upgrade their rating or something), you need to make their upper value higher, but make all the values above that bigger too so they don't overlap:

PHP Code:

function setRange($affiliateId,$newrange) {
  
$result mysql_query("SELECT * FROM affiliates WHERE id = $affiliateId");
  
$row mysql_fetch_array($result);
  
$difference $newrange-($row['upper']-$row['lower']);
  
mysql_query("UPDATE affiliates SET upper = upper + $difference, lower = lower + difference WHERE upper > ".$row['upper']);
  
mysql_query("UPDATE affiliates SET upper = upper + $difference WHERE id = $affiliateId");

That ought to do it - note this code has not been tested and may need some adjustment. Post back if you have trouble.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 04-11-2005, 02:23 AM
lajkonik86's Avatar
Ultra Talker

Posts: 389
Trades: 0
that's some really good help.
In fact, this is probably the most thorough help i ever got.
Thanks!
__________________
Know what to Download

Please login or register to view this content. Registration is FREE
lajkonik86 is offline
Reply With Quote
View Public Profile
 
Old 04-11-2005, 05:55 AM
lajkonik86's Avatar
Ultra Talker

Posts: 389
Trades: 0
the pickanaffiliate requires 2 queries.
Anybody any ideas on how to do it in one?

for getting 5 random affiliates what would be the best way?


Is there a more effective way for doing that instead of using a range?
__________________
Know what to Download

Please login or register to view this content. Registration is FREE

Last edited by lajkonik86; 04-11-2005 at 04:30 PM..
lajkonik86 is offline
Reply With Quote
View Public Profile
 
Old 04-12-2005, 10:15 AM
lajkonik86's Avatar
Ultra Talker

Posts: 389
Trades: 0
advice please
__________________
Know what to Download

Please login or register to view this content. Registration is FREE
lajkonik86 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Random affiliates
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.93061 seconds with 12 queries