[英]how to add value in existing .txt file when hit radio
For a simple voting system i use a .txt file in which the values are stored. 对于简单的投票系统,我使用.txt文件,其中存储了值。
This is the array of options: 这是一系列选项:
$quickpolloptions = ['Mozilla', 'Chrome', 'Opera', 'IE', 'Safari'];
This is the form: 这是形式:
<form method="post" id="quickpoll">
<table>
<?php
foreach ($quickpolloptions as $key => $value) {
echo "<tr>";
echo "<td>";
echo "<label>$value</label>";
echo "</td>";
echo "<td>";
echo "<input type='radio' name='radiovote' value='$key'><br>";
echo "</td>";
echo "</tr>";
}
?>
</table>
<input type="submit" value="Submit">
</form>
This is how i try to add the values in the .txt file (vote_result.txt) 这是我尝试在.txt文件中添加值的方法(vote_result.txt)
$result_file = "data/vote_result.txt";
if (file_exists($result_file)) {
$results = explode(',', file_get_contents('vote_result.txt'));
} else {
// start with zeros if you don't have a file yet
$results = array_fill(0, count($quickpolloptions), 0);
}
if (isset($_POST['radiovote'])) {
$results[$_POST['radiovote']]++;
file_put_contents('data/vote_result.txt', implode(',', $results));
}
per example: when choosing the second radio, the content of vote_result.txt looks like this: 0,1,0,0,0
This is correct. 例如:当选择第二个无线电时,vote_result.txt的内容如下所示: 0,1,0,0,0
这是正确的。 But when i vote again, lets say i chosse the 3rd radio, he overwrites the .txt file and creates this: ,1
. 但是当我再次投票时,让我说我挑选第3个收音机,他会覆盖.txt文件并创建: ,1
。 And it should be this: 0,1,1,0,0
它应该是这样的: 0,1,1,0,0
What i am doing wrong? 我做错了什么?
personally I would use serialize / unserialize for such tasks. 我个人会使用serialize / unserialize来完成这些任务。 You can save an array into a txt file and read it back + change it whenever you like, example: 您可以将数组保存到txt文件中并将其读回来+随时更改它,例如:
<?php
//initial array for a new file:
$quickpolloptions = [
'Mozilla' => 0,
'Chrome' => 0,
'Opera' => 0,
'IE' => 0,
'Safari' => 0
];
$votefile = "votes.txt";
//init the file:
if(!file_exists($votefile)){
file_put_contents($votefile, serialize($quickpolloptions));
}
//read the file and convert it back to an array:
$data = unserialize(file_get_contents($votefile));
//example of adding a vote for Mozilla:
$data['Mozilla']++;
//save back the file:
file_put_contents($votefile, serialize($data));
print_r($data);
Check below code gives same output as you want....... 检查下面的代码给出你想要的相同输出.......
<?php
$quickpolloptions = ['Mozilla', 'Chrome', 'Opera', 'IE', 'Safari'];
$result_file = "vote_result.txt";
if (file_exists($result_file)) {
$results = explode(',', file_get_contents('vote_result.txt'));
} else {
// start with zeros if you don't have a file yet
$results = array_fill(0, count($quickpolloptions), 0);
}
if (isset($_POST['radiovote'])) {
$results[$_POST['radiovote']]++;
file_put_contents('vote_result.txt', implode(',', $results));
}
?>
<form method="post" id="quickpoll">
<table>
<?php
foreach ($quickpolloptions as $key => $value) {
echo "<tr>";
echo "<td>";
echo "<label>$value</label>";
echo "</td>";
echo "<td>";
echo "<input type='radio' name='radiovote' value='$key'><br>";
echo "</td>";
echo "</tr>";
}
?>
</table>
<input type="submit" value="Submit">
</form
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.