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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kiko
Forum Newbie
Posts: 23
Joined: Fri Sep 07, 2007 6:42 am

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

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

What are your OS, database server (MySQL, PGSQL ...)?
Any code you have written?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
kiko
Forum Newbie
Posts: 23
Joined: Fri Sep 07, 2007 6:42 am

Post 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...
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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()
kiko
Forum Newbie
Posts: 23
Joined: Fri Sep 07, 2007 6:42 am

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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?
Steve Mellor
Forum Commoner
Posts: 49
Joined: Thu Aug 02, 2007 8:18 am

Post 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.
kiko
Forum Newbie
Posts: 23
Joined: Fri Sep 07, 2007 6:42 am

Post 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.
Post Reply