PHP with CVS help

Discussion in 'PHP' started by manni, Oct 11, 2012.

  1. #1
    Hello,

    I need help in getting the mean, mode, median, an total sum out of a cvs file with php, what php function would help attain these for a cvs file??


    many thanks Manni
     
    manni, Oct 11, 2012 IP
  2. kevinn13

    kevinn13 Greenhorn

    Messages:
    24
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    11
    #2
    Did you mean CSV file? comma-separated values if so you can use fopen then explode function to parse the data if its from a .csv file.
     
    kevinn13, Oct 11, 2012 IP
  3. Red Swordfish Media

    Red Swordfish Media Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here is the example for using fopen to open the file and fgetcsv to pull the data. In this example the data is echoed out line by line. But, you could just as easily place it into a SQL database and then perform the analytics on it such as mean, mode, median and total sum.

    <?php
    $row
    = 1;
    if ((
    $handle = fopen("test.csv", "r")) !== FALSE) {
    while ((
    $data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    echo
    "<p> $num fields in line $row: <br /></p>\n";
    $row++;
    for (
    $c=0; $c < $num; $c++) {
    echo
    $data[$c] . "<br />\n";
    }
    }
    fclose($handle);
    }
    ?>
     
    Red Swordfish Media, Oct 11, 2012 IP