CodeIgniter Forums
How do you loop an upload data to a table without entering empty fields? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How do you loop an upload data to a table without entering empty fields? (/showthread.php?tid=18591)



How do you loop an upload data to a table without entering empty fields? - El Forum - 05-11-2009

[eluser]dallen33[/eluser]
Here is a bit of my code:
Code:
for ($i = 1; $i <= 10; $i++)
                    {
                        $field_name = 'file'.$i;
                        $this->upload->do_upload($field_name);
                        $data = array('upload_data' => $this->upload->data());
                        $this->booking->attachments($attach_id, $data['upload_data']['file_name'], $data['upload_data']['file_type'], $data['upload_data']['file_size']);
                    }

Basically I'm uploading some files and adding the file info to a database. However, it duplicates entries if you upload less than 10 files (I have 10 upload fields). Any idea how I would only update the database if a file from the file input exists and was uploaded?


How do you loop an upload data to a table without entering empty fields? - El Forum - 05-11-2009

[eluser]dallen33[/eluser]
I figured it out:
Code:
for ($i = 1; $i <= 10; $i++)
                    {
                        $field_name = 'file'.$i;
                        $this->upload->do_upload($field_name);
                        $data = array('upload_data' => $this->upload->data());
                        if ($_FILES['file'.$i]['error']  == UPLOAD_ERR_OK)
                        {
                            $this->booking->attachments($attach_id, $data['upload_data']['file_name'], $data['upload_data']['file_type'], $data['upload_data']['file_size']);
                        }
                        else
                        {

                        }
                    }

If there's a better way of doing this, let me know. But seems to work just fine!


How do you loop an upload data to a table without entering empty fields? - El Forum - 05-12-2009

[eluser]TheFuzzy0ne[/eluser]
Any reason you can't use a foreach loop instead? Then you only loop through however many file are there. Or you make your for loop a bit like this:

Code:
for ($i = 1; $i <= count($_FILES); $i++)

But really, that's what a foreach loop is for.