-2

I have an list of names in a mysql data that I want to sort randomly for a sporting event draw. Using the following code.

<?php
 $user="";
 $password="";
 $database="";


 mysql_connect('localhost',$user,$password);
 @mysql_select_db($database) or die( "Unable to select database"); 
 $result = mysql_query("SELECT * FROM entries   ORDER BY RAND()") 
 or die(mysql_error()); 

 $rows = mysql_num_rows($result);

 ?>

This generally works well for me however there are times where an entrant enters the event twice and even after a random sort his name appears one after the other. Should i be using a different php function? Or is it a matter of adding an if clause ?

Thanks in advance...

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 5
    If you're writing new code, **_please_** don't use the `mysql_*` functions. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use PDO or `mysqli_*` _with **prepared statements** and **parameter binding** instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Dec 03 '16 at 23:45
  • 2
    "there are times where an entrant enters the event twice and even after a random sort his name appears one after the other". Yep, that's _what [random means](https://en.wikipedia.org/wiki/Randomness)_: "the lack of pattern or predictability in events". Sometimes two equal values will be beside each other. If that never happened you'd have a pattern instead of randomness. – ChrisGPT was on strike Dec 03 '16 at 23:46
  • What is the behaviour that you want? Should entrants only be able to win a single time? Should they only be able to _enter_ a single time? If they can enter multiple times, should this increase their chances of winning a prize? – ChrisGPT was on strike Dec 03 '16 at 23:51
  • You need to remove the randomly chosen entrant. So, he will not be able to get into event twice. Use an another array variable to add the randomly chosen entrant. Use `array_splice` to remove an element. This function also returns the removing element. You can store the removing element into an another array variable. – Wolverine Dec 03 '16 at 23:52
  • Yes an entrant is allowed to enter more than once .... just dont want their names to appear together want to split them up somehow. – user2432677 Dec 03 '16 at 23:59
  • @user2432677, then you'll have to define how "splitting them up somehow" works. That is _not_ random. We can't make code suggestions for unclear functionality. – ChrisGPT was on strike Dec 04 '16 at 00:02
  • Ok chris I would like to add an if statement if that is at all possible. to say if value name repeats in next row move duplicate row down one. If I can't do this perhaps I should be using a different PHP function rather than random sort? – user2432677 Dec 04 '16 at 00:08
  • 1
    That's not a PHP function. It's a MySQL function. – Charlotte Dunois Dec 04 '16 at 00:09
  • I know I am clutching at straws here but a simple yes it can be done or no it can't be answer would suffice at this stage. – user2432677 Dec 04 '16 at 00:14

2 Answers2

1

Directly implementing that condition in SQL query is not a good approach as it would become very complex, i would suggest simply fetching the result with the same SQL query and when rows are returned, $row, you use array_unique function of php, which will remove all duplicate values, use it like this,

<?php
     $row_with_no_duplicates = array_unique($rows)
 ?>

hope this helps

example of how this function works:

<?php
    $input = array("a" => "green", "red", "b" => "green", "blue", "red");
    $result = array_unique($input);
    print_r($result);
?>

Output:

Array
(
    [a] => green
    [0] => red
    [1] => blue
)

if you wish to keep the duplicates, then

$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$unique = array_unique($input ); // creating a unique array
$duplicates = array_unique( array_intersect( $input, array_unique(array_diff_key($input, array_unique($input))))); // getting duplicates

array_push($unique, $duplicates); // inserting duplicates at the end

print_r($unique);

output:

Array ( [a] => green [0] => red [1] => blue [3] => Array ( [a] => green [0] => red ) )
Talha Abrar
  • 880
  • 8
  • 22
1

One thing to consider is the constraints on your random selection. If you need to exclude someone who's already won then you need a way of recording who won and adding that to a WHERE clause somehow. Consider the following schema:

CREATE TABLE tickets (
  id INT PRIMARY KEY AUTO_INCREMENT,
  user_id INT NOT NULL,
  winner INT(1) DEFAULT 0
)

Then you can generate a number of tickets and populate the database. user_id represents the numerical ID of the potential winner. That means you can easily exclude former winners:

SELECT id FROM tickets WHERE winner=0 AND user_id NOT IN (
  SELECT user_id FROM tickets WHERE winner=1
) ORDER BY RAND()

Now keep in mind RAND() is a very, very bad random number generator and you'd never use this for something where actual prizes or money is on the line. It's just used here for simplicity. For an actual contest you'd use a cryptographically secure random number generator.

tadman
  • 208,517
  • 23
  • 234
  • 262