Possible Duplicate:
CSVs without quotes not working with fgetcsv
I would like to start by saying that I am not a PHP developer. This is the first application I've ever written in PHP. I also would like to apologize for not having much code to show.
I have a CSV file that I need to run through each row and add a new column to each record with a value that I am generating through a function call. The most PHP that I've been able to figure out is to write the function to get the extra data and open the file as 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
Again, sorry for the lack of more code, but I couldn't find too many resources for what I am doing.
Edit: Here is more code with parsing starting to come together but not quite yet. The problem that I have now is that the CSV doesn't seem to be getting parsed correctly. My CSV file looks like this (no quotes):
David Long,dave@davejlong.com
James Allen,james@davejlong.com
And my code looks like here:
<?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);
}
?>