我在主index.php文件的顶部包含了一个文件,我无法在其中创建变量(包含文件)常量(静态)
- 更多信息 - :所以我正在尝试制作表格。每次用户成功提交时,表单应该创建一个新文件(带有他的名字和他的反馈段落)。所以,我制作了一个计数器并将其附加到文件名,因此每次提交时都会生成一个新文件
static $counter = 3;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// validation code
if( /* checking that the submission is successful */){
$file = "feedback". $GLOBALS['counter'] .".txt";
file_put_contents( $file, $feedback_paragraph );
$GLOBALS[ 'counter' ] = $GLOBALS [ 'counter' ] + 1;
}
}
问题是计数器保持不变。它只会增加到4,并在再次提交时重置为3。我不知道为什么?
注意:我知道这不是实现此类项目的最佳方式,但我仍然想了解php的工作方式以及此代码无效的原因?
谢谢大家。你是一个如此神奇的社区!
答案 0 :(得分:1)
我认为这是因为你正在使用"静态"这将你的柜台重置为3。
试试这个,
提交
2.将$ counter记录到file.txt中
3.增加$ counter
4.再次提交,但这次使用file.txt中增加的计数器
5.重复步骤2~4。
答案 1 :(得分:1)
PHP无国籍。每次发出请求时,都会重新运行该脚本,在您的情况下,将计数器设置为3。
如果你想在请求之间保持计数器,你需要使用一些其他机制来保存它,就像一个单独的文件,数据库等。
答案 2 :(得分:1)
你不应该让那个变量变得静态,因为你似乎在做什么。只需在页面上使用一次就足够了,或者在必要时将其声明为全局变量(非常有助于深入研究全局变量,看起来你对它有点熟悉吗?)
只是一个问题。我不知道$ counter是否等于$ GLOBALS [' counter']。这发生在某个地方吗?
Ex:$ counter = $ GLOBALS [' counter'];或$ GLOBALS [' counter'] = $ counter;
答案 3 :(得分:1)
此代码会将文件名中的名称和反馈数据记录为“反馈(计数器值).txt”,在您的目录中创建counter.txt以进行计数。
检查您的目录并尝试为记录的数据打开“反馈(计数器值).txt”。
希望这有助于:)
<?php
//initialize
$name = "";
$feedback = "";
$counter_file = "counter.txt";
//Check if name field has input
if(isset($_POST['name']) && ($_POST['name']) != "") {
$name = $_POST['name'];
}
//Check if feedback has input
if(isset($_POST['feedback']) && ($_POST['feedback']) != "") {
$feedback = $_POST['feedback'];
}
//if name field and feedback has input call check_counter and record_counter functions
if((!empty($name)) && (!empty($feedback))) {
check_counter($counter_file);
record_data($name, $feedback, $counter_file);
}
//assuming no counter.txt is created in your directory attempt to create counter.txt
function check_counter($counter_file) {
if(file_exists($counter_file) == false) {
$file = fopen($counter_file, 'a'); //if no counter.txt file in your directory create one
$counter = 0; //initialize counter from 0
} else {
$file = fopen($counter_file, 'a+'); //if counter.txt file exist open that file
$counter = file_get_contents($counter_file); //read the counter as string
$counter++; //increment counter
}
file_put_contents($counter_file, $counter); //value of counter is recorded in counter.txt
fclose($file); //close the file
}
function record_data($name, $feedback, $counter_file) {
$file = fopen($counter_file, 'a+'); //open counter.txt file
$counter = file_get_contents($counter_file); //read the counter value of file
$txt = fopen('feedback'.$counter.'.txt', 'a'); //open file, if no file exist attempt to create it
$data = "name: ".$name.PHP_EOL."feedback: ".$feedback;
fwrite($txt, $data); //record the name,feed back value as 'feedback$counter.txt'
fclose($file); //close the file
}
?>
<html>
<head>
<title>record_data</title>
</head>
<body>
<form action="index.php" method="post">
<table>
<tr>
<td>User Name: </td><td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Feedback: </td><td><textarea name="feedback" /></textarea></td>
</tr>
<tr>
<td></td><td><input type="submit"></td>
</tr>
</table>
</form>
</body>
</html>