How to escape the 1st line to be imported into database?
Moderator: General Moderators
How to escape the 1st line to be imported into database?
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.
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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.
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.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
-
Steve Mellor
- Forum Commoner
- Posts: 49
- Joined: Thu Aug 02, 2007 8:18 am
Assuming you are loading it in as a text file using something like fopen you could explode the whole CSV file with something like:
and then for each 'record' you'd want something like this:
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.
Code: Select all
$myline = explode("\n", $contents);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++;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:
thanks for the helpful people.
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");
}