Nexus10 Posted July 13, 2010 Share Posted July 13, 2010 I need to draw a lottery somehow although am getting lost. My database has a table which has two columns: 'user' and 'numTickets' where user is there user name nad numTickets is hte number of tickets they had. I.e. the table may look like (only player's with tickets will have an entry): Player1 12 Player 2 1 Player 3 43 Player 4 7 I am having trouble manipulating this data to provide 12 tickets to Player 1, 1 ticket to Player 2 etc so we can calculate a winner etc by using the random function (to get a random number from 1 to 63 in this case here). Any suggestions (preferably with a brief sample)? Quote Link to comment https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/ Share on other sites More sharing options...
Alex Posted July 13, 2010 Share Posted July 13, 2010 Here's a quick example on one way to do it: <?php $data = array( 'user1' => 12, 'user2' => 1, 'user3'=> 43, 'user4' => 7 ); $pot = array(); foreach($data as $user => $tickets) { $pot = array_merge($pot, array_fill(0, $tickets, $user)); } shuffle($pot); echo $pot[0]; Quote Link to comment https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085214 Share on other sites More sharing options...
Nexus10 Posted July 13, 2010 Author Share Posted July 13, 2010 Thanks, although I don't really understand it fully. Is there simpler way to do this (even if its slightly longer/less elegant)? Quote Link to comment https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085218 Share on other sites More sharing options...
Nexus10 Posted July 13, 2010 Author Share Posted July 13, 2010 ? Quote Link to comment https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085232 Share on other sites More sharing options...
ignace Posted July 13, 2010 Share Posted July 13, 2010 Thanks, although I don't really understand it fully. Is there simpler way to do this (even if its slightly longer/less elegant)? Alex code creates this: Array( [0] => user1, [1] => user1, .., [11] => user1, [12] => user2, [13] => user3, .. [55] => user3, [56] => user4, .. [62] => user4 ) Then shuffle's the array and prints out the first one. You could also do: print $pot[ array_rand($pot) ]; // winner Quote Link to comment https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085249 Share on other sites More sharing options...
Nexus10 Posted July 13, 2010 Author Share Posted July 13, 2010 Okay, so this is what I've got (doesn't work): $query = "SELECT * FROM lottery_current"; $result = mysql_query($query) or die("Error: Could not be completed. <br>" . mysql_error()); $count = mysql_num_rows($result); while($row = mysql_fetch_array($result)){ echo $row['user_name']; echo $row['numTickets']; $numTicketsTotal = $numTicketsTotal + $row['numTickets']; } echo("<p>numTickets: $numTicketsTotal"); $prize = (10000 + (0.90 * 1500 * $numTicketsTotal)); echo("<p>Prize: $prize"); $result = mysql_query("SELECT * FROM lottery_current"); while($row = mysql_fetch_array($result)){ $user_name = $row['user_name']; $numTickets = $row['numTickets']; echo("<p>User name: $user_name"); echo("<p>No. of tickets: $numTickets"); $data = array($user_name => $numTickets); } $pot = array(); foreach($data as $user_name => $numTickets) { $pot = array_merge($pot, array_fill(0, $numTickets, $user_name)); } echo("<p>"); print_r($pot); //echo("<p>"); //print_r($data); shuffle($pot); echo("<p>"); echo $pot[0]; echo("<p>Done"); Which produces pillow1admin2terry1obama2 numTickets: 6 Prize: 18100 User name: pillow No. of tickets: 1 User name: admin No. of tickets: 2 User name: terry No. of tickets: 1 User name: obama No. of tickets: 2 Array ( [0] => obama [1] => obama ) obama Done The problem is, only the last user/purchaser of tickets seems to be in the array (i..e user 'obama' always wins here, no one else ever does). Can anyone see why? Quote Link to comment https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085254 Share on other sites More sharing options...
Nexus10 Posted July 13, 2010 Author Share Posted July 13, 2010 Quote Link to comment https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085267 Share on other sites More sharing options...
Nexus10 Posted July 13, 2010 Author Share Posted July 13, 2010 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085322 Share on other sites More sharing options...
Nexus10 Posted July 13, 2010 Author Share Posted July 13, 2010 I still can't get it working Quote Link to comment https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085420 Share on other sites More sharing options...
Rifts Posted July 13, 2010 Share Posted July 13, 2010 just use this http://www.awesomephp.com/?Raffle Quote Link to comment https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085426 Share on other sites More sharing options...
Nexus10 Posted July 13, 2010 Author Share Posted July 13, 2010 Well the above (not the raffle script) does what I need and I've already got 90% of it (just need to get that winner selecting right now) so I'd rather get my code above working. Quote Link to comment https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085436 Share on other sites More sharing options...
DavidAM Posted July 13, 2010 Share Posted July 13, 2010 This code: $data = array($user_name => $numTickets); } is REPLACING the contents of $data with the current record. So in the end, you only have the last record from the database. If you change it to: $data[$user_name] = $numTickets; } $pot = array(); foreach($data as $user_name => $numTickets) { $pot = array_merge($pot, array_fill(0, $numTickets, $user_name)); You should get the array in $data that you expect and the rest should work as expected. Quote Link to comment https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085453 Share on other sites More sharing options...
Alex Posted July 13, 2010 Share Posted July 13, 2010 Sorry I didn't explain the code in my first post better, it was like 4AM and I was about to go to sleep. Anyway, you can build the pot array directly within the while loop, no need to repetitious code. $pot = array(); while($row = mysql_fetch_array($result)){ $user_name = $row['user_name']; $numTickets = $row['numTickets']; echo("<p>User name: $user_name"); echo("<p>No. of tickets: $numTickets"); array_merge($pot, array_fill(0, $numTickets, $user_name)); } echo $pot[array_rand($pot)]; Quote Link to comment https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085503 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.