Php 如何在点击radio时在现有的.txt文件中添加值
对于一个简单的投票系统,我使用一个.txt文件来存储值 这是选项数组:Php 如何在点击radio时在现有的.txt文件中添加值,php,arrays,radio-button,Php,Arrays,Radio Button,对于一个简单的投票系统,我使用一个.txt文件来存储值 这是选项数组: $quickpolloptions = ['Mozilla', 'Chrome', 'Opera', 'IE', 'Safari']; 表格如下: <form method="post" id="quickpoll"> <table> <?php foreach ($quickpolloptions as $key => $value) { e
$quickpolloptions = ['Mozilla', 'Chrome', 'Opera', 'IE', 'Safari'];
表格如下:
<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>
每个示例:选择第二个收音机时,vote_result.txt的内容如下:0,1,0,0,0
这是正确的。但当我再次投票时,比如说我选择了第三台收音机,他覆盖了.txt文件并创建了这个:,1
。应该是这样:0,1,1,0,0
我做错了什么?我个人会使用serialize/unserialize来完成这些任务。您可以将数组保存到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);
我个人会使用serialize/unserialize来完成这些任务。您可以将数组保存到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);
检查下面的代码是否提供了您想要的相同输出
<?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
检查下面的代码是否提供了您想要的相同输出
<?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
尝试将数组中的数字强制转换为整数增量字符串值可能无法正常工作。找到的解决方案是键入错误:而不是:$results=explode(',',file_get_contents('data/vote_result.txt')代码>我有这样一个:$results=explode(',,file_get_contents('vote_result.txt')
我忘记了itTry cast number in array to INTEGER Increment strings value前面的文件夹data
。找到的解决方案是输入错误:而不是:$results=explode(',,file_get_contents('data/vote_result.txt')代码>我有这样一个:$results=explode(',,file_get_contents('vote_result.txt')代码>我忘了它前面的文件夹数据