Zach M.
Zach M.

Reputation: 1194

Updating totals in a CSV file PHP

I am trying to update a file that is blocked out like this

0,0,5,2,0
0,0,7,0,0
0,2,2,3,0
1,2,2,2,0
0,0,5,2,0
0,1,3,2,1
0,0,3,2,2
0,0,6,1,0

Each row is a question and each number in the row is the number of respondents. Here the code is attempting to go row by row and check which answer the user picked out of 5 radio buttons per question. So the format is something like:

Question 1: Blah              |  1  |  2  |  3  |  4  |  5  | Check one

//Grab user input from survey
$q[1] = $_POST['radio1'];
$q[2] = $_POST['radio2'];
$q[3] = $_POST['radio3'];
$q[4] = $_POST['radio4'];
$q[5] = $_POST['radio5'];
$q[6] = $_POST['radio6'];
$q[7] = $_POST['radio7'];
$q[8] = $_POST['radio8'];

//Use file handle and write to file
$FileName = "results.csv";
$FileHandle = fopen($FileName, 'a+') or die("can't open file!!");


$i = 0;

while($row = fgetcsv($FileHandle)){
    $j = 1;
    for($i = 0; $i<8; $i++){
        if($q[$j] == 1){
            $row[0]++;
        }
        else if($q[$j] == 2){
            $row[1]++;
        }
        else if($q[$j] == 3){
            $row[2]++;
        }
        else if($q[$j] == 4){
            $row[3]++;
        }
        else if($q[$j] == 5){
            $row[4]++;
        }
        $j++;
    }
}   

Upvotes: 0

Views: 287

Answers (3)

Zach M.
Zach M.

Reputation: 1194

Here is my solution to my problem, it may not be elegant but it works. The script has to pull in the values and update them at the end.

<?php

//Grab user input from survey
$q[1] = $_POST['radio1'];
$q[2] = $_POST['radio2'];
$q[3] = $_POST['radio3'];
$q[4] = $_POST['radio4'];
$q[5] = $_POST['radio5'];
$q[6] = $_POST['radio6'];
$q[7] = $_POST['radio7'];
$q[8] = $_POST['radio8'];

//Use file handle and write to file
$FileName = "results.csv";
$FileHandle = fopen($FileName, 'r') or die("can't open file!!");

$result = array();

$i = 0;
$j = 1;
while(($row = fgetcsv($FileHandle, 1024, ",")) !== FALSE){

    if($q[$j] == 1){
        $temp[0] = $row[0] + 1;
        $temp[1] = $row[1];
        $temp[2] = $row[2];
        $temp[3] = $row[3];
        $temp[4] = $row[4]; 
    }
    else if($q[$j] == 2){
        $temp[0] = $row[0];
        $temp[1] = $row[1] + 1;
        $temp[2] = $row[2];
        $temp[3] = $row[3];
        $temp[4] = $row[4];
    }
    else if($q[$j] == 3){
        $temp[0] = $row[0];
        $temp[1] = $row[1];
        $temp[2] = $row[2] + 1;
        $temp[3] = $row[3];
        $temp[4] = $row[4];
    }
    else if($q[$j] == 4){
        $temp[0] = $row[0];
        $temp[1] = $row[1];
        $temp[2] = $row[2];
        $temp[3] = $row[3] + 1;
        $temp[4] = $row[4];
    }
    else if($q[$j] == 5){
        $temp[0] = $row[0];
        $temp[1] = $row[1];
        $temp[2] = $row[2];
        $temp[3] = $row[3];
        $temp[4] = $row[4] + 1;
    }
    $result[] = $temp;
    $j++;
}
fclose($FileHandle);

$FileHandle = fopen($FileName, 'w') or die("can't open file!!");


foreach($result as $line){
    fputcsv($FileHandle, $line);
}

fclose($FileHandle);

?>

Upvotes: 0

Tom B
Tom B

Reputation: 2923

Instead of fwrite, check fputcsv() : http://php.net/manual/en/function.fputcsv.php this will give you far more flexibility than string concantenation.

e.g

$q[1] = $_POST['radio1'];
$q[2] = $_POST['radio2'];
$q[3] = $_POST['radio3'];
$q[4] = $_POST['radio4'];
$q[5] = $_POST['radio5'];
$q[6] = $_POST['radio6'];
$q[7] = $_POST['radio7'];
$q[8] = $_POST['radio8'];

fputcsv($FileHandlem $q);

Upvotes: 1

DaveRandom
DaveRandom

Reputation: 88657

Escape sequences such as \n are not interpolated within single quotes.

You have to use double quotes:

$result = $q1.','.$q2.','.$q3.','.$q4.','.$q5.','.$q6.','.$q7.','.$q8."\n";

Also, you should take a look at the fputcsv() function.

Upvotes: 1

Related Questions