Jump to content

trying to get correct counts


msimonds

Recommended Posts

the following code is supposed to get the counts of records coming in and keep count of the records:

 

$count

$good_count

$bad_count

 

 

here is the code:

 

$count = count($data);
    for ($i = 0; $i < $count; $i++)
    {
        //Process the records through business rules
        $validate = validate_record($data[$i]);
        //If an error is returned then write it out
        $bad_count = 0;
        if ($validate['ERROR'] != null)
        {
            //add headder to error output CSV file
            if (!$error_headers_written)
            {
                $keys = array_keys($validate);
                fputcsv($fp,$keys);
                $error_headers_written = true;
            }
            fputcsv($fp,$validate);
            
            $bad_count++;
        }
    }
    //count check for DR reporting purposes
    $good_count = $count - $bad_count;

 

$count works perfectly because it is just counting the number of records in a file

 

$bad_count is only supposed to increment if the $validate['error'] is not null

 

$good_count would work if I could get $bad_count to render the correct count

 

 

so can someone basically look at this part of the code:

 

$bad_count = 0;
        if ($validate['ERROR'] != null)
        {
            //add headder to error output CSV file
            if (!$error_headers_written)
            {
                $keys = array_keys($validate);
                fputcsv($fp,$keys);
                $error_headers_written = true;
            }
            fputcsv($fp,$validate);
            
            $bad_count++;
        }

 

and please tell me the dumb mistake that I am making

Link to comment
https://forums.phpfreaks.com/topic/96038-trying-to-get-correct-counts/
Share on other sites

$bad_count will never go above 1. You define it as 0 within your loop... therefor every time the loop executes, it resets to 0.

 

try moving it outside the loop, or put static in front of the declaration

 

static $bad_count = 0

if (....

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.