Welcome Guest, Not a member yet? Register   Sign In
Help w/ updating backend db with .csv file
#1

[eluser]puddles[/eluser]
Hi,

I'm a novice programmer, and need help. I've used the file uploading class from codeigniter to upload a hockey player statistics file, in .csv format. Now, I'd like to somehow read that file, parse it, and update a backend mysql db with each row of the data.

I'm having trouble with some very basic things:

1) Understanding where the logic goes - in the view or in the controller?
2) What is the easiest way to take an array of a row's data (which has 31 fields), and insert it into a database using ActiveRecord?
3) Is there a way to add array items to each row so that I can supplement the data that was provided in the actual .csv file?

Code:
$row = 1;
$handle = fopen('D:/xampp/htdocs/newhphq/daily_stats/NHL2010GoldPlayers_100809.reg_.csv', 'r');
    // iterate through file one row at a time until receive FALSE    
    while (($rawdata = fgetcsv($handle,",")) !== FALSE)
    {
            
    // how do I add new items to the array

            
    // how do I use ActiveRecord to insert array into database

                
    // increment row counter to next row
    $row++;
                
       //
                                    
}
fclose($handle);

Any help or advice is greatly appreciated. Thank you.
#2

[eluser]pistolPete[/eluser]
[quote author="puddles" date="1255289050"]1) Understanding where the logic goes - in the view or in the controller?[/quote]
Have a look at the user guide: http://ellislab.com/codeigniter/user-gui...w/mvc.html
There should be little to no logic in the view!


[quote author="puddles" date="1255289050"]2) What is the easiest way to take an array of a row's data (which has 31 fields), and insert it into a database using ActiveRecord?[/quote]
If the array is associative, it's as simple as
Code:
$this->db->insert($array);
User guide: active_record.html#insert


[quote author="puddles" date="1255289050"]3) Is there a way to add array items to each row so that I can supplement the data that was provided in the actual .csv file?[/quote]
I don't know if I understood you correctly, but you can serialize an array and save the result to the database.
http://php.net/manual/en/function.serialize.php




Theme © iAndrew 2016 - Forum software by © MyBB