0

I have a csv file I need to cleanup. It contains 13 fields, but I only need 7 (Business, Address, City, St, Zip, Phone, Email)

I need to run through all of the records and create a new output of just the records with email addresses.

In nutshell... I load the original file, run the for loop, explode the results, then look for the records where the $tmp[10] index is not null. I then get the rest of the rest of required fields, and do a foreach loop and fwrite the results to a new csv file.

Depending on how I tweak the code, I get either... A text file of just email addresses. or A text file of just the last record with an email address.

I have been working on this too long and I just need a fresh set of eyes to point out the problem. I am new to php, and want to make this work. Thanks on advance.

    <?php
    // See if file exists and is readable
    $file = 'uploads/AK_Accountants.csv';
    $newfile = basename($file,".csv");
    $newfile = $newfile.Date("Ymd").".csw";
    $fileNew = fopen('uploads/AK_'.Date("Ymd").'.csv','w+');
    // Read the file into an array called $sourcefile
    $sourcefile = file($file);
    // Loop through the array and process each line
    for ($i = 0; $i < count($sourcefile); $i++) {
        // Separate each element and store in a temp array
       $tmp = explode('","', $sourcefile[$i]);
        // Assign each element of the temp array to a named array key
        if ($tmp[10] != "") {
            $sourcefile[$i] = array('Business_Name' => $tmp[1], 'Address' => $tmp[3], 'City' =>  $tmp[4], 'State' => $tmp[5], 'Zip' => $tmp[6], 'Phone' => $tmp[7], 'Email' => $tmp[10]);
            foreach($sourcefile[$i] as $key => $value);
                fwrite($fileNew, $value);
        }
    }
    ?>
3
  • 3
    You say you've been working on this code a long time but actually this code is from page 187 of PHP solutions, dynamic web design made easy by David Powers and you haven't really added much to it at all. ;) Commented Sep 28, 2011 at 20:15
  • Your suggestion helped. Thank you Commented Sep 28, 2011 at 20:22
  • 1
    This is the comment section. Answers are below. To comment on an answer, click the add comment link directly below the answer. Commented Sep 28, 2011 at 20:28

2 Answers 2

3

From a quick glance:

foreach($sourcefile[$i] as $key => $value);
    fwrite($fileNew, $value);

should be

foreach($sourcefile[$i] as $key => $value){
    fwrite($fileNew, $value);
}

Also, you have

$newfile = $newfile.Date("Ymd").".csw";

rather than what I assume should be

$newfile = $newfile.Date("Ymd").".csv";
Sign up to request clarification or add additional context in comments.

Comments

0

Your last foreach statement is terminated by a ';' and has no code block. Once the foreach statement has finished iterating you'll get the last value written to file i.e. just the email address.

You currently have

foreach (... ) ;
fwrite(...);.

but you probably mean

foreach( ... ) {
    fwrite(...) ;
}

Been there, done that :)

HTH

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.