Php 向CSV文件中添加列
可能重复:Php 向CSV文件中添加列,php,csv,Php,Csv,可能重复: 首先,我想说我不是一名PHP开发人员。这是我用PHP编写的第一个应用程序。我还想为没有太多代码显示而道歉 我有一个CSV文件,我需要在每一行中运行,并向每一条记录添加一个新的列,其中包含我通过函数调用生成的值。我能理解的最简单的PHP就是编写函数来获取额外的数据,并以CSV的形式打开文件 $csvFile = fopen('records.csv'); $csv = fgetcsv($csvFile); foreach($csv as $line){ // Create
首先,我想说我不是一名PHP开发人员。这是我用PHP编写的第一个应用程序。我还想为没有太多代码显示而道歉 我有一个CSV文件,我需要在每一行中运行,并向每一条记录添加一个新的列,其中包含我通过函数调用生成的值。我能理解的最简单的PHP就是编写函数来获取额外的数据,并以CSV的形式打开文件
$csvFile = fopen('records.csv');
$csv = fgetcsv($csvFile);
foreach($csv as $line){
// Create a new code to populate the new column with:
$linkCode = getUniqueCode();
// Add the column to the record
// Save the record back to the csv
}
// Save the CSV back to a file
再次为缺少更多的代码感到抱歉,但是我找不到太多的资源来支持我所做的工作
编辑:这里有更多的代码,解析开始结合在一起,但还不是很好。我现在的问题是CSV似乎没有得到正确的解析。我的CSV文件如下所示(无引号):
我的代码如下所示:
<?php
//require_once "Mail.php";
//require_once "Mail/mime.php";
$facebook = '*****';
$twitter = '*****';
$band = '*****';
$album = '******';
$paypal = '******';
$download = '******';
function makeConnection(){
$host=$_ENV{'DATABASE_SERVER'};
$username="*******";
$password="*********";
$db_name="**********";
$con = mysql_connect("$host", "$username", "$password")or die(mysql_error());
mysql_select_db("$db_name")or die(mysql_error());
return $con;
}
function makeCode($email, $name){
$codeused = true;
do{
$code = substr(microtime().'_'.$GLOBAL['album'],2,6);
$escapeCode = mysql_escape_string($code);
$query = mysql_query("SELECT * FROM downloads WHERE code='$code'")or die(mysql_error());
if(!mysql_num_rows($query))$codeused = false;
else $codeused = true;
}while($codeused);
$email = mysql_escape_string($email);
$name = mysql_escape_string($name);
$download = mysql_escape_string($GLOBALS['download']);
$query = mysql_query("INSERT INTO downloads(email,name,code,download) VALUES('$email','$name','$code','$download')")or die(mysql_error());
return 'http://********'.$code;
}
$conn = makeConnection();
if(($csvFile = fopen('emails.csv','r')) !== false && ($resultCsv = fopen('result.csv','w')) !== false){
while (($data = fgetcsv($csvFile)) !== false){
$data[] = makeCode($data[1],$data[0]);
echo $data;
fputcsv($resultCsv, $data);
}
fclose($csvFile);
fclose($resultCsv);
}
?>
类似这样的内容:
ini_set('auto_detect_line_endings',TRUE); // Fixes problem with Mac Excel
if (($csvFile = fopen("records.csv", "r")) !== false && ($resultCsv = fopen('result.csv', 'w')) !== false) {
while (($data = fgetcsv($csvFile)) !== false) {
$data[] = getUniqueCode();
fputcsv($resultCsv, $data);
}
fclose($csvFile);
fclose($resultCsv);
}
谢谢你。现在,如果我想从csv中获取一个列,我该如何做?例如,调用$data[]=getUniqueCode($col1,$col2);?尝试
$data[]=getUniqueCode($data[0],$data[1])谢谢,我找到了。但现在我注意到,您的代码似乎无法处理Excel中的CSV文件。我只将1个代码添加到最后一条记录中(共2条),当我将记录放入数据库时,我将2个字段放入1。我将发布更多的代码,上面是我到目前为止所拥有的。我在这篇帖子上找到了我的答案:
ini_set('auto_detect_line_endings',TRUE); // Fixes problem with Mac Excel
if (($csvFile = fopen("records.csv", "r")) !== false && ($resultCsv = fopen('result.csv', 'w')) !== false) {
while (($data = fgetcsv($csvFile)) !== false) {
$data[] = getUniqueCode();
fputcsv($resultCsv, $data);
}
fclose($csvFile);
fclose($resultCsv);
}