Page 1 of 1

How to escape the 1st line to be imported into database?

Posted: Fri Sep 07, 2007 6:54 am
by kiko
Hi all,
Does anyone know about how to escape the first line data of the csv file to be imported into database and how to read cell by cell (not line by line) of the csv file ? I appreciate any helps. Thanks.

Posted: Fri Sep 07, 2007 8:42 am
by VladSun
What are your OS, database server (MySQL, PGSQL ...)?
Any code you have written?

Posted: Fri Sep 07, 2007 8:45 am
by John Cartwright
Assuming you have already gotten your csv into an array, you can apply array_shift() to remove the first element of the array

Posted: Mon Sep 10, 2007 12:49 am
by kiko
Yup, I had got the csv into an array, but when I use the array_split(), it'll still write the whole array into another csv file(which I intended to write into-I named it"insert.csv"). Sorry, I'm not so familiar with PHP...

Posted: Mon Sep 10, 2007 12:53 am
by jmut
kiko wrote:Yup, I had got the csv into an array, but when I use the array_split(), it'll still write the whole array into another csv file(which I intended to write into-I named it"insert.csv"). Sorry, I'm not so familiar with PHP...
array_shift() != array_split()

Posted: Mon Sep 10, 2007 4:21 am
by kiko
So, you mean should use array_split() to split it? Thanks a lot. But I have another concern is: I'm getting the data from a csv file using two dimensional array but it only can be get one character once in one cell. How to get the whole one cell's data?
For example:

data.csv

ID | name |
1234 | kiko |
2222 | alain |
1111 | Wilson |

If I use $id[0][3], then I'll only get 4, that's not what I expected.
Does anyone here know about this? Thanks.

Posted: Mon Sep 10, 2007 7:36 am
by superdezign
Doesn't sound like it's a two dimensional array at all. Sounds like it's an array of strings. Have you done a print_r() / var_dump() on it?

Posted: Mon Sep 10, 2007 8:19 am
by Steve Mellor
Assuming you are loading it in as a text file using something like fopen you could explode the whole CSV file with something like:

Code: Select all

$myline = explode("\n", $contents);
and then for each 'record' you'd want something like this:

Code: Select all

$myrow = explode ("|", $myline[$i]);

$id = trim($myrow[0]);

$name = trim($myrow[1]);

/*
Perform any actions you want with your 'id' and 'name' variables.
*/

$i++;
Stick that in the loop of your choice (remembering to set $i to zero before the loop starts) and then perform whatever actions you want on the variables.

Posted: Mon Sep 10, 2007 9:03 pm
by kiko
Hi all,
Thanks for reply. jmut, there's no function called array_split(). I had echo the result and see but also the same as I done before, it will only come out whole column data but not a single cell's data. Now my code is like below:

Code: Select all

while($data = fgetcsv ($f_open,2000, ",")) 
{ 
  $num = count($data); 

  fwrite($handle, $data[0]."\r\t\n"); 
  fwrite($handle, $data[1]."\r\t\n");//$handle is the new csv file that I want to insert after read a csv file
/*actually what I want is to $data[0] and $data[1] in the same row but in diiferent cell, but I can't make it coz when I write it into  another new csv file(excel), it will only write into the same cell*/ 
                                                                                                        //(same column). 
  
 for ($c=2; $c < $num; $c++) 
{ 
  fwrite($handle, $data[$c]."\r\n"); 
}
thanks for the helpful people.