更新CSV文件PHP中的总计
我正在尝试更新一个像这样被阻止的文件更新CSV文件PHP中的总计,php,csv,Php,Csv,我正在尝试更新一个像这样被阻止的文件 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 每一行都是一个问题,每一行中的数字都是回答者的数量。这里的代码试图逐行检查用户从每个问题的5个单选按钮中选择的答案。因此,格式类似于: Question 1: Blah | 1 | 2 | 3 | 4 | 5 | Check one //Grab us
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
每一行都是一个问题,每一行中的数字都是回答者的数量。这里的代码试图逐行检查用户从每个问题的5个单选按钮中选择的答案。因此,格式类似于:
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++;
}
}
问题1:Blah | 1 | 2 | 3 | 4 | 5 |勾选一个
//从调查中获取用户输入
$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'];
//使用文件句柄并写入文件
$FileName=“results.csv”;
$FileHandle=fopen($FileName,'a+')或die(“无法打开文件!!”);
$i=0;
而($row=fgetcsv($FileHandle)){
$j=1;
对于($i=0;$i等转义序列,如\n
不在单引号内插入
必须使用双引号:
$result = $q1.','.$q2.','.$q3.','.$q4.','.$q5.','.$q6.','.$q7.','.$q8."\n";
此外,您还应该查看函数。检查fputcsv(),而不是fwrite:这将比字符串连接提供更大的灵活性
e、 g
这是我的问题解决方案,它可能不优雅,但它可以工作。脚本必须拉入值并在最后更新它们
<?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);
?>
噢,谢谢。我会仔细阅读,我甚至不知道这个函数存在。它工作得非常好,所以下一步是,如果我错了,请阻止我:读入csv的每一行->找到更新正确条目的方法->将该行写回正确位置以更新文件。因此,我将逐行更新检查值例如,如果q1=3
这意味着一个中性响应,因此如果第一行0,0,0,0,0
在我更新后,它应该变成0,0,1,0,0
。为什么在保存之前需要读取CSV?当然你应该用更新的信息编写CSV?也许我处理问题的方法不对。我必须增加值在文件中,q的值是它需要更新的位置,所以我想我必须选择所有的值,检查q的值是什么,并更新该行中的位置。q1是文件的第一行,它包含的数字是需要更新的列。我上面的测试示例实际上不是我想要完成的,这只是为了搞清楚CSV。我还在为我的新问题寻找解决方案,试图只更新从fgetcsv中拉入的行值。
<?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);
?>