Php Lottery Problems

I created a lottery script in php. My problem is now choosing more than one winner. Because players can have the same number on their tickets. Here I supply two table structures and source code.

lotto_game {
    id(int)
    jackpot(int)
    status(varchar10)
    pick_1(int)
    pick_2(int)
    pick_3(int)
    pick_4(int)
    pick_5(int)
    tickets_sold(int)
    winner(text)
}

 lotto_picks {
    lotto_id(int)
    user_id(int)
    choice_1(int)
    choice_2(int)
    choice_3(int)
    choice_4(int)
    choice_5(int)
    ticket_status(int)
}

These are my two tables in my database. For example, we will create 2 users with identifiers 1 and 2. So, what happens when the script is launched, it is assumed that it will change the status of lotto_game from "active" to "finished", and then add random lottery numbers to each pick_ *.

$one = rand(1,30);
$two = rand(1,30); 
$three = rand(1,30);
$four = rand(1,30);
$five = rand(1,30);

mysql_query("UPDATE `lotto_game` SET 
 pick_1 = '$one',
 pick_2 = '$two',
 pick_3 = '$three',
 pick_4 = '$four',
 pick_5 = '$five',
 status = 'finished' 

WHERE status = 'active' ");

It was not too difficult, I admit. But this is only the beginning of the end.

$lotto['tickets'] = mysql_query("SELECT ticket_id FROM `lotto_picks` WHERE ticket_status='valid'");

@$lotto[winners] = mysql_query("SELECT ticket_id,user_id FROM `lotto_picks` WHERE choice_1 = '$one' AND choice_2 = '$two' AND choice_3 = '$three' AND choice_4 = '$four' AND choice_5 = '$five'");

$lotto['num_tickets'] = mysql_num_rows($lotto['tickets']);
@$lotto[winner_id] = mysql_fetch_array(@$lotto[winners]);
$lotto['jackpot'] = mysql_query("SELECT jackpot FROM `lotto_game` WHERE status='active'");

$lotto['winner_jackpot'] = mysql_fetch_array($lotto['jackpot']);
$lotto['num_winners'] = mysql_num_rows($lotto['winners']);
//echo @$lotto['num_tickets'];
//echo @$lotto['num_winners'];
$winner = $lotto['num_winners'];
//echo @$lotto['winner_id']['user_id'];
$jackpot = $lotto['winner_jackpot']['jackpot'];
$id = @$lotto[winner_id][user_id];
if ($winner == 1) {
    mysql_query("UPDATE `character` SET
    decivers = decivers +'$jackpot'
WHERE user_id='$id'");
}

, , , . , . , . , , , .

, .

- , - . , , , ticket_status "" ""

+3
2
$winners_array = array();
if(mysql_num_rows($lotto['winners'])!=0){
  while($row =mysql_fetch_array($lotto['winners'])){
    if(!in_array($row['user_id'],$winners)) $winners[] = $row['user_id'];
  }
}

$winners user_ids

+1

, . , N- , N.

: "2 4 5 9 11". 1 "10100011010", 1306. "4 7 9 12 13", "1100101001000" == 6472. , :

SELECT BIT_COUNT(1306 & 6472)

, 2 . "" :

SELECT * FROM tickets WHERE BIT_COUNT(tickets.pick & lotto.pick) = 5

SELECT * FROM tickets ORDER BY BIT_COUNT(tickets.pick & lotto.pick) DESC
+2

Source: https://habr.com/ru/post/1726100/


All Articles