Welcome Guest, Not a member yet? Register   Sign In
parsing csv to feed values in to a database
#1

[eluser]stef25[/eluser]
I'm trying to use CI to extract values from a csv file and feed them in to a database. Ive managed to use CI classes to upload the csv files from my pc to a location on my server – all good.

In my upload.php I have the code below which outputs the relevant values from the array on my view page, upload_sucess.php using the second block of code
Code:
if(! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('upload_form',$error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            $this->load->view('upload_success', $data);
                        
        }



Code:
<? foreach($upload_data as $item => $value): ?>
<li>&lt;?=$item;?&gt;: &lt;?=$value;?&gt;</li>
&lt;? endforeach; ?&gt;

So far so good.

I now have a piece of code that parses the csv file into an array of values. Id like to extract those values and feed them in to my db. Im thinking I should insert this code in the else {} block above … but im not sure how to proceed. Can anyone tell me what my next step should be? Much appreciated!

Code:
$fp = fopen("path/to/file.csv", "r");
while (!feof ($fp)) {
$contents[] = explode(",", fgets($fp));
}
fclose ($fp);




Theme © iAndrew 2016 - Forum software by © MyBB