Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6718363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:57:36+00:00 2026-05-26T08:57:36+00:00

Possible Duplicate: CSVs without quotes not working with fgetcsv I would like to start

  • 0

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);
        }
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:57:37+00:00Added an answer on May 26, 2026 at 8:57 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: Why C# implements methods as non-virtual by default? It would be much
Possible Duplicate: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result what is
Possible Duplicate: or is not valid C++ : why does this code compile ?
Possible Duplicate: Can we change the device time using an application? is that possible
Possible Duplicate: string1 >= string2 not implemented in Linq to SQL, any workarround? To
Possible Duplicate: Why do I see a double variable initialized to some value like
Possible Duplicate: How to rename java.exe/javaw.exe process? Hi, I am working on java desktop
Possible Duplicate: In PHP (>= 5.0), is passing by reference faster? I wonder if
Possible Duplicate: upload a file to server without using a form? I'am able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.