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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.