0

This code works perfectly except that it doesn't print the last 2 rows of my csv file:

This is the file:

603629,0,ATLV0008,"Vendor1",1942.60,11/04/2010,1942.60,9/1-9/30/10,EFT-JP
603627,2,ATLV0008,"Vendor1",1242.40,11/04/2010,1242.40,7/1-7/31/10,EFT-JP
600023,0,FLD4V0003,"Vendor2",1950.00,06/15/2010,1950.00,6/14/10 Request,EFT-JP
600024,0,FLD4V0003,"Vendor2",1800.00,06/15/2010,1800.00,6/14/10 Request,EFT-JP
603631,0,ATLV5066,"Vendor2",1000.00,11/09/2010,1000.00,11/4/10 Check Request,PM2
603647,0,ATLV5027,"DVendor3",2799.80,11/15/2010,2799.80,10/1-10/31/10 Bishop,PM2
603642,5,ATLV5027,"Vendor3",482.40,11/15/2010,482.40,10/1-10/18/10 Allen,PM2
603653,0,ATLV0403,"Vendor4",931.21,11/17/2010,931.21,9/1-9/30/10,EFT-JP
603661,0,ATLV0105,"Vendor5",26.75,11/19/2010,26.75,093139,PM2
603660,1,ATLV0105,"Vendor5",5.35,11/19/2010,5.35,093472,PM2

Here is the code: (It needs to display the sum of 2 rows with the same vendor before the actual rows)

if (($handle = fopen('upload/ATLANTA.csv', "r")) !== FALSE) {
  while (($row_array = fgetcsv($handle, 1000, ","))) { 
    foreach ($row_array as $key => $val) {
        $row_array[$key] = trim(str_replace('"', '', $val));
        ob_flush();
        }
    $complete[] = $row_array;
    ob_flush();
    }
}
flush();

$prevVendor = '';
$sum = 0;
$venData = '';

    if (isset($complete)) {

        foreach ($complete as $key => $val) {
            $venData .= "<br/>";   
            $currVendor = $complete[$key][3];   

            if ($currVendor != $prevVendor){

                    if ($prevVendor != NULL){
                        print "<br/>";
                        print $sum . "<br/>";
                        print $venData;
                        $sum = 0; $venData = '';
                    }
                }   

        foreach ($val as $ikey => $ival){
            if ($ikey != 1){
                $venData .= $ival . '&nbsp';
                $prevVendor = $val[3];
                }
        }  

         if ($currVendor == $prevVendor){
                $sum += $complete[$key][6];
            }    
    }

}

5
  • 6
    Please try to improve your rate of acceptance on your other questions. Commented Dec 17, 2010 at 18:33
  • if you ask a question, and someone answers it correctly, mark it so Commented Dec 17, 2010 at 18:56
  • (click the tick by the most helpful post in your previous questions) Commented Dec 17, 2010 at 19:05
  • I Recommend Better Indention And Trying To Build A Rational Loop That Iterate Once Over the csv file data Commented Dec 17, 2010 at 19:12
  • Could there be a better way of doing this? Commented Dec 19, 2010 at 3:54

1 Answer 1

1

I don't really understand your problem but if you want to get (and save it wherever you want) the sum of each vendors, you should do something like this :

$prevVendor = null;
$venData = '';
$sums = array();

if (isset($complete) && is_array($complete)) {
    $lim = count($complete);
    for ($key=0;$key<$lim;++$key) { // no need foreach for simple array
        $venData .= "<br/>";   
        $currVendor = $complete[$key][3];   

        if ($currVendor != $prevVendor){

            if ($prevVendor != null){
                print "<br/>";
                print $venData;

            }

            $venData = '';
            $prevVendor = $currVendor;
            $sums[$currVendor] = 0; // set the counter to 0

        }   

        foreach ($complete[$key] as $ikey => $ival){
            if ($ikey != 1){
                $venData .= $ival . '&nbsp;'; // is useful for you I guess
            }
        }

        $sums[$currVendor] += $complete[$key][6]; // add amounts

    }
}

if ($prevVendor != null){ // you need to do this to display the last record
    print "<br/>";
    print $venData;

}
var_export($sums); // check results

This can be some syntax errors ...

Sign up to request clarification or add additional context in comments.

3 Comments

The sum calculation is working fine, the problem is that the last two lines of the file do not get printed, since the printing happens at the beginning and the looping happens at the end. The reason I have to keep it that way is because I need the sum calculation to happen before the printing.
I still don't get it ... Add this after the loop? : if ($prevVendor != NULL){ print $sum . "<br/>"; }
Add this after the loop : if ($prevVendor != null){ print "<br/>"; print $venData; }

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.