[nycphp-talk] Random
Mark Armendariz
nyphp at enobrev.com
Fri Jan 16 11:23:35 EST 2004
Just a follow up, here's how I implemented the random number generator for
my query (using adodb):
------------ snip --------------
require_once('includes/function.random.php');
// Total Members
$members_count = $db->GetRow('SELECT'
. '
COUNT(member_id) as member_count'
. ' FROM '
.
TABLE_MEMBERS . ' m'
. ' WHERE
m.contest_subscribed = 1'
. ' AND
m.contest_selected = 0'
. ' AND
m.member_enabled = 1');
// Generate random number from random.org
$random_user_number = GetRandomNumber(1, $members_count['member_count'], 1);
// Winner
$winner = $db->GetRow('SELECT'
. '
m.member_id'
. ' FROM '
.
TABLE_MEMBERS . ' m'
. ' WHERE
m.contest_subscribed = 1'
. ' AND
m.member_enabled = 1'
. ' AND
m.contest_selected = 0'
. ' ORDER BY
m.member_username'
. ' LIMIT ' .
($random_user_number - 1) . ', 1');
// Make sure there's no current winner
$db->Execute('UPDATE ' . TABLE_MEMBERS
. ' SET'
. ' contest_rocbox_current = 0'
, ' WHERE 1');
// Update the winner to make sure they can only win once
$db->Execute('UPDATE ' . TABLE_MEMBERS
. ' SET'
. ' contest_selected = 1,'
. ' contest_date = NOW(),'
. ' contest_current = 1'
. ' WHERE member_id = ' .
$winner['member_id']);
------------ snip --------------
Thanks for the help!!!!
Mark
More information about the talk
mailing list