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
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 (....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.