[英]How to add all numbers in txt file and make into a variable with a value?
Basically, I'm trying to make it where when the button is pressed, it will log 1
into data.txt then PHP will add up the 1
's and display that number for all users on-page to see.基本上,我试图让它在按下按钮时将1
记录到 data.txt 然后 PHP 将添加1
并显示该数字以供页面上的所有用户查看。 I'm trying to do this in complete PHP but I can use javascript and HTML if needed.我正在尝试在完整的 PHP 中执行此操作,但如果需要,我可以使用 javascript 和 HTML。 Here is my complete code:这是我的完整代码:
<?php
$newline = "\n";
$filename = 'data.txt';
$somecontent = ("1");
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, "$somecontent $newline") === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
<html>
<head>
<title>__Work-In-Progress__</title>
</head>
<form action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method = post>
<input type = "submit" name = "button" value = "Submit" >
</form>
</html>
It's kinda messy but ahh it gets the job done.这有点乱,但它完成了工作。 Thanks in advance!提前致谢!
EDIT: Site is here but I'm not finished with the CSS and all.>> http://clickthebutton.rf.gd/index.php编辑:网站在这里,但我还没有完成 CSS 和所有。>> http://clickthebutton.rf.gd/index.php
Yep i got it: Here is my full code:是的,我明白了:这是我的完整代码:
<?php
$newline = "\n";
$filename = 'data.txt';
$somecontent = ("1");
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, "$somecontent $newline") === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
} else {
echo "The file $filename is not writable";
}
$no_of_lines = count(file($filename));
echo "The button has been pressed $no_of_lines times."
?>
<html>
<head>
<title>__Work-In-Progress__</title>
</head>
<form action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method = post>
<input type = "submit" name = "button" value = "Submit" >
</form>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.