Jump to content

Put csv file contents into text area and choose random line


giraffemedia

Recommended Posts

Hello,

 

I want to put together a little script that does the following but I'm not sure how to approach it…

 

1 - user chooses a csv file up to 5mb in size filled with competition entries. There is one competition entry per line.

2 - The contents of this csv then load into a html textarea on the page

3 - The user clicks a 'pick winner' button and the script chooses a random line from the textarea and assigns it to the variable $winner which prints to the browser

4 - If the user wishes they can click on the button to select more winners if necessary.

 

I'm a bit stumped about the best way to do this. I don't really want to get into uploading the csv file to a directory permanently.

 

Can anyone point me in the right direction for doing this please?

 

Thanks,

 

James

The only time the file is stored permanently is if you actively move the file in the php script. You can read from the file using the temporary file location. The location can be found with something like:

 

$tmp_name = $_FILES["formfield"]["tmp_name"];
$fh = fopen($tmp_name, 'r');

if ($fh !== FALSE) {
    while (($data = fgetcsv($fh, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($fh);
}

 

Something like that should do what you are looking for.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.