2011-04-17 44 views
0

我是新的PHP和我目前正在一个涉及csv的项目。我没有任何编写脚本来读取csv文件的经验。所以请不要忽视我的帖子,请原谅我。CSV阅读第一行,编辑它,然后存储到数据库 - PHP

摘要:该任务要求我使用php脚本读取CSV文件,重命名第一行(标题,并插入数据库)。

我对PHP代码不是很熟悉,所以我很感激任何帮助,而这正是我想要做的。

的CSV的实施例的数据文件

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 

创建一个函数,它读取CSV文件。

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 
} 

我已经完成脚本来解析csv文件到mysql中。但我不知道如何重命名/重新格式化csv数据。请帮助我,提前致谢,

+2

你应该有你的代码。 – 2011-04-17 06:12:48

+0

您的问题真的是CSV中的列名与数据库表的列名不符?如果是这样,我只需创建一个查找数组作为配置,将CSV中的名称映射到表列名称。 – DanMan 2011-04-17 09:55:40

回答

2

不知道你的分析需要什么,但我建议使用fgetcsv进行解析。你如何操纵并推回到数据库取决于你。

fputcsv对写回文件也很有用。这两个文档页面都有使用示例。

0

您不需要编写整个代码来解析并获取值到数组中。您可以使用ParseCSV库来完成此操作。您可以将这些值传递给数组并对其进行处理。

相关问题