I was given a task to find a random name picker software for an raffle but most of what i find was too complicated with database backend or too limited by the number of the participants. So i decide to try and code something by myself and I end up with this:
It's web based PHP random array key pickup. Limited by the "upload_max_filesize" and "max_execution_time" in php.ini . I successful test it with 5mil records(1.29GB csv file) which can be found here http://eforexcel.com/wp/downloads-16-sample-csv-files-data-sets-for-testing/
Here is the web page:
index.php
download.php
Better approach will be to generate number from 1 to max rows and then fetch the rows based of the row number instead of storing the results in an array and compare them for duplicates.
Another random list generator can be found here https://www.random.org/lists/ it's limited to 10 000
Edit:
fixed missing html tags and duplicate detection.
It's web based PHP random array key pickup. Limited by the "upload_max_filesize" and "max_execution_time" in php.ini . I successful test it with 5mil records(1.29GB csv file) which can be found here http://eforexcel.com/wp/downloads-16-sample-csv-files-data-sets-for-testing/
Here is the web page:
index.php
PHP:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="author" content="Versus">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
table {
display: table;
border-collapse: collapse;
border-spacing: 2px;
border-color: gray;
}
#results {
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#results td, #results th {
border: 1px solid #ddd;
padding: 8px;
}
#results tr:nth-child(even){background-color: #f2f2f2;}
#results tr:hover {background-color: #ddd;}
#results th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #04AA6D;
color: white;
}
</style>
</head>
<body>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<table border="1" width="600">
<tr>
<td width="20%">Select File</td>
<td width="80%"><input type="file" name="file" id="file" /></td>
</tr>
<tr>
<td width="20%">Number of rewards</td>
<td width="80%"><input type="number" name="draw" id="draw" /></td>
</tr>
<tr>
<td>Submit</td>
<td><input type="submit" name="btn_submit" value="Upload File" /></td>
</tr>
</table>
</form>
<?php
session_start();
if (isset($_FILES['file'])) {
// Check for upload error
if ($_FILES['file']['error'] > 0) {
echo "Return Code: " . $_FILES['file']['error'] . "<br />";
}
else {
// print details
echo "File: " . $_FILES['file']['name'] . "<br />";
echo "Note: Generated using PHP function <a href='https://www.php.net/manual/en/function.array-rand.php'>array_rand()</a><br />";
echo "Size: " . round(($_FILES['file']['size'] / 1024), 2) . "KB<br />";
echo "Number of rewards: " . $_POST["draw"] . "<br />";
}
// Check for file access
if (($handle = fopen($_FILES['file']['tmp_name'], 'r')) !== FALSE) {
$data = file($_FILES['file']['tmp_name']);
$total = count($data);
echo "Total Rows: " . $total . "<br />";
echo "<hr>";
$num = (int)$_POST["draw"];
$result = array();
// check for number of rewards
if ($num > $total) {
echo "Number of rewards must be less than total rows.";
fclose($handle);
return 0;
}
echo "<table id='results'>";
echo "<tr>";
echo "<th>Winners</th>";
echo "</tr>";
// draw N random rows from array
for ($c=0; $c < $num;) {
$line = str_replace(array("\r\n", "\n", "\r"), '',$data[array_rand($data)]);
// check for duplicate
if (in_array($line, $result)) {
continue;
}
// store result in array, remove LF CR
$result[] = $line;
echo "<tr>";
echo "<td><b>" . ($c+1) . ": </b>" . $line . "<br /></td>";
echo "</tr>";
$c++;
}
echo "</table>";
$_SESSION['result'] = $result;
fclose($handle);
}
}
?>
<br />
<form action="download.php" method="post">
<input type="submit" name="download" value="Download" />
</form>
</body>
</html>
download.php
PHP:
<?php
session_start();
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
$result = $_SESSION['result'];
$fp = fopen('php://output', 'w');
fputcsv($fp, $result, "\r");
fclose($fp);
?>
Better approach will be to generate number from 1 to max rows and then fetch the rows based of the row number instead of storing the results in an array and compare them for duplicates.
Another random list generator can be found here https://www.random.org/lists/ it's limited to 10 000
Edit:
fixed missing html tags and duplicate detection.
Last edited: