I have a code to roll a rand number, and show the winner based on their chance.
$data = array();
foreach($getAllUserTicketHistoryJson as $value){
$data[$value['user_id']] = number_format((float)($value['total_ticket'] / $getAllTicketRound * 100), 2, '.', '');
}
$array=$data;
function chance($input=array())
{
$number=rand(0,array_sum($input));
$starter=0;
foreach($input as $key => $val)
{
$starter+=$val;
if($number<=$starter)
{
$ret=$key;
break;
}
}
return 'Winner is '.$ret.'<br/>';
}
for($i=0;$i<3;$i++)
{
echo chance($array).'<br><br>';
}
This will give output as below.
Winner is 4
Winner is 3
Winner is 4
The problem is, user "4" winner twice in first round and last round. How to prevent the winner win twice?
and how to insert each winner to a database?
my database look like this
=========================================
id | Round | first | Second | third
=========================================
1 | 1 | 4 | 3 | 1
=========================================
I want to insert each winner to database "first", "second", and "third". So the winner ID will not double to prevent the repetition.
please help.