Welcome Guest, Not a member yet? Register   Sign In
CSV upload, once uploaded to a temp folder overwrite the existing database
#20

[eluser]TheFuzzy0ne[/eluser]
This is untested, but should work.

I've made a few improvements by removing some the redundant code (the first two lines), and now your insert is made in a single query, rather than hitting the database once per insert.

Code:
// The start of the insert string. We just add to this with our loop.
        $insert_str = 'INSERT INTO wc_program_temp (JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish) VALUES '."\n";

        // Open the file in read only mode and get a handle.
        $fh = fopen("http://localhost/sussex/assets/uploads/wc_program.csv", "r");

        if ($fh) {
            // Create each set of values.
            while (($csv_row = fgetcsv($fh, 1000, ',')) !== false) {

                foreach ($csv_row as &$row) {
                    $row = strtr($row, array("'" => "\\'", '"' => '\\"'));
                }

                $insert_str .= '("'
                    // Implode the array and fix pesky apostrophes.
                    .implode('","', $csv_row)
                    .'"),'."\n";
            }

            // Remove the trailing comma.
            $insert_str = rtrim($insert_str, ",\n");

            // Insert all of the values at once.
            $this->db->query($insert_str);

            $this->load->view('program/success');
        } else {
            echo "Failed";
        }

It could probably do with being tweaked a bit more, but I'll leave that to you. Smile


Messages In This Thread
CSV upload, once uploaded to a temp folder overwrite the existing database - by El Forum - 03-24-2013, 11:48 AM



Theme © iAndrew 2016 - Forum software by © MyBB