raDiO_acTivE Posted November 30, 2008 Share Posted November 30, 2008 Hello again This is another question to do with the same script that i talked about in my previous thread but this is just a question on using less code and making it more efficient. So the Weekly cron im running this script in ( a raffle script ) is the following piece of code. <?php $select = $db->query('SELECT * FROM `raffle`'); $this = $db->num_rows($select); $that = $db->fetch_row($select); $win = rand(1,$this); $win1 = sprintf("SELECT `userid` FROM `raffle` WHERE `id` = %d",$win); $win2 = $db->query($win1); $user = $db->fetch_row($win2); $earnings = sprintf("UPDATE `users` SET `money` = `money` + %d WHERE `userid` = %u",$that['pot'],$user['userid']); $db->query($earnings); event_add($user['userid'],"You won the weekly lottery and were credited \${$that['pot']}",$c); $insert = sprintf("INSERT INTO `rafflelog` (rID,rDATE,rUSER,rAMNT) VALUES (%d, unix_timestamp(),%u,%d)", '', $userid, $that['pot']); $db->query($insert); $db->query("TRUNCATE TABLE `raffle`"); $db->query("UPDATE `users` SET `lottery` = 0"); ?> That there is the code that runs every friday night. I was thinking of expanding it so it would choose three random winners however, is there i way i can do this without having to run three of everything..Like instead of doing something like this. <?php //Query to select three winners. $select = $db->query('SELECT * FROM `raffle`'); $this = $db->num_rows($select); $that = $db->fetch_row($select); $win = rand(3,$this); $win1 = sprintf("SELECT `userid` FROM `raffle` WHERE `id` = %d",$win); $win2 = $db->query($win1); //Three winners $user = $db->fetch_row($winnerq1); $user2 = $db->fetch_row($winnerq1); $user3 = $db->fetch_row($winnerq1); //How much they win forumla $1win = 7/10 * $that['pot'] ; $2win = 2/10 * $that['pot'] ; $3win = 1/10 * $that['pot'] ; //Send money to first winner $earnings = sprintf("UPDATE `users` SET `money` = `money` + %d WHERE `userid` = %u", $1win, $user['userid']); $db->query($earnings); //Send money to 2nd winner $earn = sprintf("UPDATE `users` SET `money` = `money` + %d WHERE `userid` = %u", $2win, $user['userid']); $db->query($earn); //Send money to 3rd winner $ea = sprintf("UPDATE `users` SET `money` = `money` + %d WHERE `userid` = %u", $3win, $user['userid']); $db->query($ea); //Event Ads event_add($user['userid'],"You won the weekly lottery and were credited \$$1win",$c); event_add($user2['userid'],"You won the weekly lottery and were credited \$$2win",$c); event_add($user3['userid'],"You won the weekly lottery and were credited \$$3win",$c); //Raffle Log $insert = sprintf("INSERT INTO `rafflelog` (rID,rDATE,rUSER,rAMNT) VALUES (%d,%d,%u,%d)", ''; unix_timestamp(); $user; $that['pot']); $db->query($insert); $db->query("TRUNCATE TABLE `raffle`"); $db->query("UPDATE `users` SET `lottery` = 0"); ?> Im no advanced php coder but i try my best. So im not sure if there is a way to do this without having 3 of everything. If you can help me i appreciate it =) Thankyou PS. That second piece of code is not tested at all. Im certain it will not work. Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 30, 2008 Share Posted November 30, 2008 SELECT `userid` FROM `raffle` ORDER BY RAND() LIMIT 3 will give you three random 'userid's And yes, you could use arrays and for loops to remove some of the redundant code Quote Link to comment Share on other sites More sharing options...
raDiO_acTivE Posted November 30, 2008 Author Share Posted November 30, 2008 Thankyou, i might need a little guidance on that but ill give it a go and report how i go soon. Quote Link to comment Share on other sites More sharing options...
raDiO_acTivE Posted December 1, 2008 Author Share Posted December 1, 2008 SELECT `userid` FROM `raffle` ORDER BY RAND() LIMIT 3 will give you three random 'userid's And yes, you could use arrays and for loops to remove some of the redundant code Using that query gives three random userids as you said, however, i need to specify the three users as each winner wins a different amount out of the pot, they do not all receive the same amount? Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 1, 2008 Share Posted December 1, 2008 Process them in while() loop with counter. Let the first one get 0.7 of the pot, second one 0.2 and the last one 0.1 Quote Link to comment Share on other sites More sharing options...
raDiO_acTivE Posted December 2, 2008 Author Share Posted December 2, 2008 Hmmm im having a bit of trouble here. Could someone post the base code of a for() loop with a counter? Ive been trying with the following base code. <?php //Your starting number & Ending number $start_point = 0; $end_pont = 5; for($i = $start_point; $i < $end_point; $i++) //What we do here is the following: // $i = $start_point == your starting number // $i = $end_point == your ending number // $i++ == counts to that number to stop //Start the loop { //echo out your restult echo $i.'<br>'; //close the loop } ?> Is that correct? Quote Link to comment Share on other sites More sharing options...
raDiO_acTivE Posted December 2, 2008 Author Share Posted December 2, 2008 BUMP Quote Link to comment Share on other sites More sharing options...
raDiO_acTivE Posted December 2, 2008 Author Share Posted December 2, 2008 Anyone? Quote Link to comment Share on other sites More sharing options...
unkwntech Posted December 2, 2008 Share Posted December 2, 2008 That is technically correct. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.