Welcome Guest, Not a member yet? Register   Sign In
Just a nice csv upload and populate the database function
#1

[eluser]srpurdy[/eluser]
Been working on a project for a statistics database, with a friend of mine, but maybe this code can help someone trying to get csv data into a database. Thanks to Jeff for his help too. I'm still learning. Wink I attached the csv library code I used for this as well. Which I didn't write. I don't know how many people are still using csv files but it was needed for this project.

I gotta move some of this code into a model, so its a bit of mess right now. But hopefully not too bad. Smile When I do that ill post a cleaned up version. Smile

insert function
Code:
function insert_results()
{
    //Upload file
      $config['upload_path'] = './public/results';
      $config['allowed_types'] = 'csv|tsv';
      $config['max_size']  = '1024';
      $replace = '"';
      $with = ' ';

      $this->load->library('upload', $config);
    
      if(!$this->upload->do_upload())
      {
        $data['error'] = array('error' => $this->upload->display_errors());
            
        $data['page'] = $this->config->item('FAL_template_dir').'template_admin/results/addresults';
        print_r($this->upload->data());
               $this->load->vars($data);
            
           $this->load->view($this->_container);
      }
      else
      {

    //Insert file info into database
    $data = array('upload_data' => $this->upload->data());
    $userfile = $data['upload_data']['file_name'];
      }
      $array = array(
    'event_id' => $this->input->post('event_id'),
    'name' =>  $this->input->post('name'),
    'datetime' =>  $this->input->post('datetime'),
    'userfile' =>  $userfile
        );
    $this->db->set($array);
    $this->db->insert('results');

    //Insert CSV Data into database
    $this->load->library('csvreader');
    $filePath1 = './public/results/';
    $filePath2 = $data['upload_data']['file_name'];
    $filePath = $filePath1 . $filePath2;
    $data['csvData'] = $this->csvreader->parse_file($filePath);
    foreach($data['csvData'] as $cd){
                       $results_array = array(
                               'event_id' => $this->input->post('event_id'),
                               'Fin_Pos' => str_replace($replace, $with, $cd['"Fin Pos"']),
                               'CarID' => str_replace($replace, $with, $cd['"Car ID"']),
                               'Car' => str_replace($replace, $with, $cd['"Car"']),
                               'Driver' => str_replace($replace, $with, $cd['"Driver"']),
                               'Start_Pos' => str_replace($replace, $with, $cd['"Start Pos"']),
                               'CarNumber' => str_replace($replace, $with, $cd['"Car #"']),
                               'Out' => str_replace($replace, $with, $cd['"Out"']),
                               'Interval' => str_replace($replace, $with, $cd['"Interval"']),
                               'LapsLed' => str_replace($replace, $with, $cd['"Laps Led"']),
                               'AvgLap' => str_replace($replace, $with, $cd['"Average Lap Time"']),
                               'FastLap' => str_replace($replace, $with, $cd['"Fastest Lap Time"']),
                               'FLNumber' => str_replace($replace, $with, $cd['"Fast Lap#"']),
                               'LapsComp' => str_replace($replace, $with, $cd['"Laps Comp"']),
                               'Inc' => str_replace($replace, $with, $cd['"Inc"'])
                       );        
               $this->db->set($results_array);
               $this->db->insert('results_data');
               }
redirect('./admin/results');
}

library
http://www.codeigniter.com/wiki/CSVReader

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CSVReader Class
*
* $Id: csvreader.php 147 2007-07-09 23:12:45Z Pierre-Jean $
*
* Allows to retrieve a CSV file content as a two dimensional array.
* The first text line shall contains the column names.
*
* @author        Pierre-Jean Turpeau
* @link        http://www.codeigniter.com/wiki/CSVReader
*/
class CSVReader {

    var $fields;        /** columns names retrieved after parsing */
    var $separator = ',';    /** separator used to explode each line */

    /**
     * Parse a text containing CSV formatted data.
     *
     * @access    public
     * @param    string
     * @return    array
     */
    function parse_text($p_Text) {
        $lines = explode("\n", $p_Text);
        return $this->parse_lines($lines);
    }

    /**
     * Parse a file containing CSV formatted data.
     *
     * @access    public
     * @param    string
     * @return    array
     */
    function parse_file($p_Filepath) {
        $lines = file($p_Filepath);
        return $this->parse_lines($lines);
    }
    /**
     * Parse an array of text lines containing CSV formatted data.
     *
     * @access    public
     * @param    array
     * @return    array
     */
    function parse_lines($p_CSVLines) {
        $content = FALSE;
        foreach( $p_CSVLines as $line_num => $line ) {
            if( $line != '' ) { // skip empty lines
                $elements = split($this->separator, $line);

                if( !is_array($content) ) { // the first line contains fields names
                    $this->fields = $elements;
                    $content = array();
                } else {
                    $item = array();
                    foreach( $this->fields as $id => $field ) {
                        if( isset($elements[$id]) ) {
                            $item[$field] = $elements[$id];
                        }
                    }
                    $content[] = $item;
                }
            }
        }
        return $content;
    }
}
#2

[eluser]codex[/eluser]
Or you could just use LOAD DATA INFILE. Way quicker and easier.
#3

[eluser]ray73864[/eluser]
and depending on what you are doing, LOAD DATA INFILE will always be quicker, especially if you are comparing what you are importing with what is already in the DB and updating where necessary.
#4

[eluser]projectom[/eluser]
you save my day! thanks




Theme © iAndrew 2016 - Forum software by © MyBB