0

I'm creating a simple lottery script.

The idea is that in one lottery there could be a few winners and I'm having troubles with checking if a new winner is a person who already won in this lottery.

I store this kind of data in DB.

list [longtext] - column with a list of contestants (separated with spaces or comas) winner [longtext] - column with a list of winners in this lottery (separated with spaces)

My loop:

        //$won_this is person who won in this round
        $old_winners = $draw[winner];

        $czy = strpos($old_winners, "$won_this");

        while($czy == FALSE)
        {
         $add_winner = $won_this;
        }

        $sql = "update `draws` set `winner`= concat(winner, ' $add_winner') where code='$draw['number']'";
        mysql_query($sql) or die(mysql_error());

My loop doesn't work. It will loop forever or not at all. I have no idea how to write this.

How can I create a loop that runs when a winner is duplicated and works until the new winner is found?

Michał Lipa
  • 968
  • 2
  • 12
  • 22
  • See [Is storing a delimited list in a database column really that bad?](http://stackoverflow.com/q/3653462). – eggyal Mar 07 '15 at 12:11
  • do not store comma separated values in the db, use another table for saving winners.. it will help you more managing your module – Nishant Solanki Mar 07 '15 at 12:12

2 Answers2

1

The first thing I would do is convert the old winners into an array:

$winners = explode(' ', $draw['winner']);

Then I would add the new winner to the array:

$winners[] = $won_this;

And finally I would call array_unique on the array to ensure uniqueness and then convert the array back into a string to be inserted into the database:

$winners_string = implode(' ', array_unique($winners));

$stmt = $connection->prepare("update `draws` set `winner`= ? where code = ?");

// Use bing_param('si'...) if $draw['number'] is an integer, not a string
$stmt->bind_param('ss', $winners_string, $draw['number']);

$stmt->execute();

Although ideally, and as mentioned in the comments to your question, there are better ways to store the data, e.g. have a new table with a draw_number column and a winner column and simply add a new row for each winner.

Michael
  • 11,912
  • 6
  • 49
  • 64
  • Thanks, but my main problem is ensuring uniqueness. I don't know how to do this. How to construct loop. – Michał Lipa Mar 07 '15 at 15:32
  • The uniqueness is achieved by calling `array_unique` on the `$winners` array as shown above. I'll edit my answer to draw better attention to it. – Michael Mar 07 '15 at 16:04
  • The problem with your code is that it doesn't look for another winner when it found out a duplicate. So user can click to start lottery and dont get any value because script found duplicate, so he will need to start lottery again. – Michał Lipa Mar 07 '15 at 17:06
0

$czy is always false so nothing will happen in this script. It is always false because you are using the wrong syntax to search the array. Change your solution for checking your array Michael example is correct. Try it

Maze Runner
  • 244
  • 2
  • 10