Jump to content

Help with parsing/manipulating .CSV


jmann1

Recommended Posts

Greetings!

 

I am trying to create a print auditing app that will display how much each user is printing on a daily basis.

 

I am using Novell iPrint for auditing. The audit report is in .csv format.

The trouble I am having is every print job is logged individually. See below.

 

USER_1,5,54022

USER_1,1,54506

USER_1,24405

USER_1,1,18943

USER_1,1,12947

USER_1,3,12947

USER_1,1,76747

USER_1,1,18943

 

USER_2,4,456130

 

USER_3,1,99425

USER_3,1,98308

USER_3,1,170148

USER_3,3,129331

USER_4,1,283553

USER_4,1,430808

 

The first field represents username, the second field represents page count for that job, the third field represents total bytes for that job.

 

I want to combine each user into a single row and then sum the page count and byte count for that user.

For example:

 

USER_1,15,273460

 

USER_2,4,456130

 

USER_3,8,1211573

 

The csv file is written to each time a print job is submitted.

I can open the file and parse the data while it's in use, but I am lost as far as manipulating the data as mentioned above.

The first line in the csv also needs to be completely ignored because it contains info about the file, time, etc.

 

Any help or coding examples would be greatly appreciated.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/162797-help-with-parsingmanipulating-csv/
Share on other sites

Forgot to add what little code I already have:

 

<?php

 

$audit_file = fopen('vol1:ndps/4891E9F4.PSM/audit/audit.csv','r');

echo "<table border='1' cellpadding='2' cellspacing='1' width='100%' style='font-family: Arial; font-size: 14'>\n";

while($line = fgetcsv($audit_file,1024)) {

    echo "<tr>";

    for ($i = 0, $j = count($line); $i < $j; $i++) {

        echo "<td nowrap>".$line[$i]."</td>";

    }

    echo "</tr>";

}

echo "</table>";

 

fclose($audit_file);

 

?>

Before starting the table, I would load the whole file into an array keyed on the username.  Something like:

$psum = array();
while($line = fgetcsv($audit_file,1024)) {
    $usr = $line[0];
    if (isset($psum[$usr])) {
        $psum[$usr]['pages'] += $line[1];
    } else {
        $psum[$usr] = $line[1];
    }
}

 

Then inside the table, I would output the $psum array

Getting the following:

Fatal error: Cannot use assign-op operators with overloaded objects nor string offsets in audit.php on line 9

 

This is line 9:

$psum[$usr]['pages'] += $line[1];

 

 

Before the error, it was outputting a table resembling:

 

Pages Pages Pages

1      1      1

1      1      1

1      1      1

 

None of the other csv data was being displayed.

 

 

Hmm, I've never seen that error before.  But I don't do much math in PHP.  My first guess would be to try intval() around the $line[1] in both places.  If that didn't work, I'd try putting the assignment in "longhand"; $psum[$usr]['pages'] = $psum[$usr]['pages'] + $line[1]; and maybe put intval() around $line[1] there, and then maybe put it around the $psum ... on the right ...

 

Sorry, maybe someone with more experience can chime in.

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.