我在php中创建了一个彩票脚本。我的问题是现在选择一个以上的赢家。因为玩家可以在他们的门票上拥有相同的号码。这里我提供了两个表结构和源代码。
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)
}
这是我的数据库中的两个表。例如,我们将创建id为1和2的2个用户。所以当脚本运行时会发生这种情况,假设将lotto_game状态从“active”更改为“finished”,然后将随机抽奖号码添加到每个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'“);
我承认这并不太难。但这只是结束的开始。
$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'");
}
这就是我想出来的,它似乎与一位胜利者合作。但我只是想弄清楚从哪里开始。我试过使用一些数组,但没有任何作用。我知道需要做些什么,但无法弄清楚如何去做。
当我搜索获胜者时,我需要将所有用户ID放入数组中。
如果有人对此感到困惑,那么额外的决定就是金钱。门票上的状态在这里并不重要,但如果您必须知道它只是确定ticket_status是“有效”还是“无效”答案 0 :(得分:3)
我认为您为所选数字选择了错误的存储格式。标准方法是使用如果选择数字N则设置了第N位的二进制值。
考虑这个例子:用户选择数字“2 4 5 9 11”。将相应的位设置为1给出'10100011010',即十进制1306.现在,乐透选择“4 7 9 12 13”,即“1100101001000”== 6472.对两个值执行按位AND并计算在中设置的位数结果:
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
答案 1 :(得分:1)
$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
的数组