I need to upload a csv file from a form. That I can do. The csv file contains the names and emails of people who entered the contest. To get the contest winners, I need the php to read the file, randomize the rows, and print out the entire row.
I have been trying with the following code that I found here on stackoverflow, but it only outputs one column from a random row. I need it to output the entire row, and I need three rows. No matter what I try, I can't get this code to do it:
<?php
if (!isset($_FILES)){exit;}
$file = $_FILES['file'];
$file_handle = fopen($file['tmp_name'], "r");
$line_of_text = array();
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
// Random Row and Column
$random_row = array_rand($line_of_text);
$random_column = array_rand($line_of_text[$random_row]);
//Random row, with all three columns
$column = 1;
$column = 2;
$column = 3;
echo $line_of_text[$random_row][$column-1][$column-2][$column-3];
?>
If I remove column 2 and 3, it prints column 1, but I need all three columns, the entire row. I also want to grab three random rows and print out each row on a line.
$line_of_text[$random_row]
in this case is an array. Assuming I understand its structure correctly, you could easily get all three columns by doing the following:
echo $line_of_text[$random_row][$column-1] . $line_of_text[$random_row][$column-2] . $line_of_text[$random_row][$column-3];
This will concatenate the values of the three array values together. If you want to separate them via spaces, just replace the .
with a . ' ' .
or similar.
Thank you, the concatenation worked! For some reason, I had to change the order with column 3, then, 2, then one. But that gets me the results I want.