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?