Jump to content

Recommended Posts

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)?

Link to comment
https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/
Share on other sites

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];

Link to comment
https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085214
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085249
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085254
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085453
Share on other sites

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)];

Link to comment
https://forums.phpfreaks.com/topic/207576-drawing-a-lottery/#findComment-1085503
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.