views:

44

answers:

3

I am to import a file, say june.txt that would have data such as the following data:

    Sandy,820,384,133,18,408
    Wanda,120,437,128,807,595
    Jane,631,415,142,687,600
    Andrea,179,339,349,594,986
    Wanda,803,191,6,807,322
    Jane,741,975,34,15,832
    Jane,239,714,250,94,497
    Andrea,219,188,411,584,713

And then the PHP would parse it into 2 difference ways:

The first way being all the names bundled together with totals, such as:

    Sandy   820     384     133     18      408
    Total   820     384     133     18      408


    Jane    631     415     142     687     600
    Jane    741     975     34      15      832
    Jane    239     714     250     94      497
    Total   1611    2104    426     796     497


    Andrea  179     339     349     594     986
    Andrea  219     188     411     584     713
    Total   398     527     760     1178    1699

    Wanda   120     437     128     807     595
    Wanda   803     191     6       807     322
    Total   923     628     134     1614    917

The second way would total and add the names together in a big list, such as

    Sandy   820     384     133     18      408
    Jane    1611    2104    426     796     497
    Andrea  398     527     760     1178    1699
    Wanda   923     628     134     1614    917

Any logic or suggestions would be helpful, I am new to PHP and not sure how this could even be done. My plan is to eventually display the results in HTML tables and have them sortable, but I can tackle that at a later date, Unless someone feels obligated to just add the and such for me in the parsing.

A: 

Well, to start, I'd real a file like that with PHP's fgetcsv(). Docs.

alex
A: 

You could try a script like this:

<?php
echo "<table>";

$data = file("june.txt");
foreach($data as $month) {
    $line = explode(',', $data);
    echo '<tr><td>', implode('</td><td>', $line), '</td></tr>';
}

echo "</table>";

Edit: My bad, didn't notice that you were sorting/grouping/totaling. This should set you on the right track, though. The key is to use $line as your source of information. Just compile it into an array and output later (instead of right in the loop).

mattbasta
+1  A: 

I think something useful for you would be the explode function.

As far as creating these views I'd start by loading all this data into an associative array of arrays based on the name, then iterate as necessary:

$datafile = file("filename.txt");

// reads lines into an associative array (key is the name) of arrays
// where each sub-array is a list of the records for each name
$arr = array();
foreach($datafile as $line){
    $temp = explode(',', $line);
    $arr[$temp[0]][] = $temp;
}

// iterate over each person
foreach($arr as $person_set){

    // create an array to hold the sum of each column 
    // (and the name in the first column)
    $totals = array();
    $totals[0] = $person_set[0][0];
    for($i = 1; $i < length($record); $i++){
        $totals[$i] = 0;
    }

    // now iterate over each record for this person
    foreach($person_set as $record){

        // print a particular record
        echo implode(' ', $record) . '<br>';

        // add each column (1..end) to the totals array
        for($i = 1; $i < length($record); $i++){
            $totals[$i] += $record[$i];
        }
    }

    // print out the totals line
    echo implode(' ', $totals) . '<br><br>';
}

I'll leave formatting this data into a table as an exercise.

Mark E
This is awesome and I have formatting the data to a table. It seems you have actually made it so it totals every column and row to 1 number for reach grouping, whereas I wanted to total only the column, as in my example
BHare
I got of the //add each column (1..end) for loop and replaced it with: $totals[i] += $record[1]; $totals[i+1] += $record[2]; $totals[i+2] += $record[3]; $totals[i+3] += $record[4]; $totals[i+4] += $record[5];... Seems to do what I want. I am not sure that the i (without the $) represents.
BHare
@Brian, it's a typo, should be $totals[$i] everywhere it was $total[i], see my edit.
Mark E