• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you loop an upload data to a table without entering empty fields?

#1
[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?

#2
[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!

#3
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.