开发者

CSV reading first line, editing it and then store into database - PHP

I'm new with php and I am currently working on a project which involve of csv. I do not have any past experience of writing a script to read csv files. So please do not ignore my post and do excuse me.

Summary: The task require me to read a CSV file with php script, rename the 1st line (Title, and insert into database).

I am not very familiar with php code, so I would appreciate any help, and this is what I would like to do.

Example data of the CSV File

Name Contact ID Email Address Status
Alice 0134222 21 alice@gmail.com 92 alice st available
claire 013423 24 claire@gmail.com 1 young st avail
victor 023429 31 victor@gmail.com 15/8 johnson st not available

create a function that reads a CSV file.

function parse_csvdata($filename) {
  //read the first line of the csv file only (the title)
  //rename the title to User_Name, User_Contact, User_ID (instead of Name, Contact, I开发者_Go百科D)

  //temporary store it in an array as TITLE

  //read the rest of the data (after the first line)
  //check the data in the title column. I want to change all avail to Available

  then create a new CSV out of this
}

I already finish the script to parse csv files into mysql. But I just dunno how to rename/reformat the csv data. Please help me, thanks in advance,


Not sure what your parsing entails, but i recommend using fgetcsv for parsing. How you manipulate and push back to a database is up to you.

fputcsv is also useful for writing back to the file. Both doc pages have examples of usage.


You don't need to write the entire code to parse and get the values into an array.You can use ParseCSV library to do it. You can take the values to an array and manipulate it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜