2

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);
        }
?>
Community
  • 1
  • 1
Dave Long
  • 9,569
  • 14
  • 59
  • 89

1 Answers1

2

Something like this:

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);
}
Dave Long
  • 9,569
  • 14
  • 59
  • 89
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • Thanks xdazz. Now if I wanted to get one of the columns from the csv how would I do that? for example to call $data[] = getUniqueCode($col1, $col2);? – Dave Long Oct 20 '11 at 04:41
  • Try `$data[] = getUniqueCode($data[0], $data[1]);` – xdazz Oct 20 '11 at 04:45
  • Thanks I found that. But now I am noticing that your code doesn't appear to work with CSV files out of Excel. I only get 1 code added to the last record (out of 2) and when I put the records into a database I get 2 fields put into 1. I will post more code above for what I have so far. – Dave Long Oct 20 '11 at 05:08
  • Nevermind. I found my answer on this post: http://stackoverflow.com/questions/2321064/csvs-without-quotes-not-working-with-fgetcsv – Dave Long Oct 20 '11 at 05:13