Xavien
Xavien

Reputation: 15

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 [email protected] 92 alice st available
claire 013423 24 [email protected] 1 young st avail
victor 023429 31 [email protected] 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, ID)

  //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,

Upvotes: 0

Views: 1094

Answers (2)

Shameer
Shameer

Reputation: 3066

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.

Upvotes: 0

Brad Christie
Brad Christie

Reputation: 101614

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.

Upvotes: 2

Related Questions