将数组写入带有 id 的文件?

发布于 2024-12-12 08:59:09 字数 1508 浏览 0 评论 0原文

你能帮我将表单中选定的值及其 ID 写入文件吗?我有 html 表单:

<form name="frm1" method="post" action="form_to_write.php">
    <h4>q1</h4>
    <input type="radio" name="answer[1]" value="someValue1" />someValue1<br />
    <input type="radio" name="answer[1]" value="someValue2" />someValue2<br />
    <input type="radio" name="answer[1]" value="someValue3" />someValue3
    <h4>q2</h4>
    <input type="radio" name="answer[3]" value="someValue4" />someValue4<br />
    <input type="radio" name="answer[3]" value="someValue5" />someValue5<br />
    <input type="radio" name="answer[3]" value="someValue6" />someValue6
    <h4>q3</h4>
    <input type="radio" name="answer[2]" value="someValue9" />someValue9<br />
    <input type="radio" name="answer[2]" value="someValue7" />someValue7<br />
    <input type="radio" name="answer[2]" value="someValue8" />someValue8
    <input type="submit" value="submit" name="submit"/>
</form>

我的文件 form_to_write.php 是:

$array = array_map("rtrim", $_POST['answer']);
$str = implode("\r\n", $array);
$date="date_not_work"; //date('m-d-y_H:i:s')
$fp = fopen($date."_out.txt", "w+");
$write=fwrite($fp, $str);
fclose($fp);

if ($write) echo 'Write is done!';
else echo 'Error!';

我需要这样的文件结果:

1|value of 1
3|value of 3
2|value of 2

现在它只写入值。

Can you help me to write selected values from a form to a file with their id. I have html form:

<form name="frm1" method="post" action="form_to_write.php">
    <h4>q1</h4>
    <input type="radio" name="answer[1]" value="someValue1" />someValue1<br />
    <input type="radio" name="answer[1]" value="someValue2" />someValue2<br />
    <input type="radio" name="answer[1]" value="someValue3" />someValue3
    <h4>q2</h4>
    <input type="radio" name="answer[3]" value="someValue4" />someValue4<br />
    <input type="radio" name="answer[3]" value="someValue5" />someValue5<br />
    <input type="radio" name="answer[3]" value="someValue6" />someValue6
    <h4>q3</h4>
    <input type="radio" name="answer[2]" value="someValue9" />someValue9<br />
    <input type="radio" name="answer[2]" value="someValue7" />someValue7<br />
    <input type="radio" name="answer[2]" value="someValue8" />someValue8
    <input type="submit" value="submit" name="submit"/>
</form>

And my file form_to_write.php is:

$array = array_map("rtrim", $_POST['answer']);
$str = implode("\r\n", $array);
$date="date_not_work"; //date('m-d-y_H:i:s')
$fp = fopen($date."_out.txt", "w+");
$write=fwrite($fp, $str);
fclose($fp);

if ($write) echo 'Write is done!';
else echo 'Error!';

I need result in file like this:

1|value of 1
3|value of 3
2|value of 2

Now it write only values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心小果奶 2024-12-19 08:59:09
$str = '';
foreach ($_POST['answer'] as $num => $answer) {
  $str .= "$num|$answer\r\n"; //i see no point in doing rtrim here
}

而不是

$array = array_map("rtrim", $_POST['answer']);
$str = implode("\r\n", $array);

日期,可能是因为 : 符号。

设为“ym-d_H-is” 格式。
请注意 ymd 顺序。这更可靠,因为它使您的文件可以按日期排序

$str = '';
foreach ($_POST['answer'] as $num => $answer) {
  $str .= "$num|$answer\r\n"; //i see no point in doing rtrim here
}

instead of

$array = array_map("rtrim", $_POST['answer']);
$str = implode("\r\n", $array);

as for the date, it's probably because of : symbols.

make it "y-m-d_H-i-s" format.
note the y-m-d order. this is way more reliable as it makes your files sortable by date

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文