如果任何人可以帮助我在PHP中我将非常感谢。我是一个新的PHP和我正在研究一个项目,我的部分是使用文本框获取输入并将其保存到文本文件带时间戳。 我能够做到这一点,我的代码工作正常。 但我无法做一些要求,例如让计数器输入了多少条目,如果条目是重复的,则不要添加到文件中。我尝试包含一个计数器,它总是显示1作为文件重新加载每次。我想要一个文本框再次获得更多的条目,我正在重新加载它。
<h1>please scan your student id card :)</h1>
<form action = "barcodeHandler.php" method = "POST">
<input type = "text" name = "barcode" style= "width:400PX; height:40PX;opacity:10" autofocus>
<input type = "submit" style= "width:0px; height:0px;opacity:0;" value = "barcodeSubmit" >
</form>
<?php
$counter = 0;
//$filePath = getenv("HOMEDRIVE").getenv("HOMEPATH")."\Desktop"; // use this when using windows will give user desktop dir
//$filePath = "/Applications/XAMPP/xamppfiles/htdocs/PHP_Lessons/project"; // setting file directory
$filePath = "./";
$course = "MCI";
$extensionOfFile = ".txt";
$dateForFile = date ("d_m_Y");
$fileName = "/$course"."_"."$dateForFile"."$extensionOfFile "; // name of the file
if(!empty($_POST['barcode']))
{
$barcode = $_POST['barcode']; // getting barcode
$time = time(); // unix time stamp
$actual_time = date('H:i:s',$time); // actual time
$file = $filePath.$fileName;
$entry = $barcode .", ".date ("d-m-Y"). " ".$actual_time; // putting in the data in variable
$handle = fopen("$file",'a'); // open a file
fwrite($handle,$entry."\r\n"); // "/r/n" will give a line break
fclose($handle); // free the file
//echo date_default_timezone_get(); // if required to check the timezone of the server
$counter++;
}
echo $counter;
header("location :barcodeHandler.php"); // redirect to the form
?>
答案 0 :(得分:2)
您的计数器只会返回1,因为它不会计算文件中已存在的条目。
<h1>please scan your student id card :)</h1>
<form action = "barcodeHandler.php" method = "POST">
<input type = "text" name = "barcode" style= "width:400PX; height:40PX;opacity:10" autofocus>
<input type = "submit" style= "width:0px; height:0px;opacity:0;" value = "barcodeSubmit" >
</form>
<?php
$counter = 0;
//$filePath = getenv("HOMEDRIVE").getenv("HOMEPATH")."\Desktop"; // use this when using windows will give user desktop dir
//$filePath = "/Applications/XAMPP/xamppfiles/htdocs/PHP_Lessons/project"; // setting file directory
$filePath = "./";
$course = "MCI";
$extensionOfFile = ".txt";
$dateForFile = date ("d_m_Y");
$fileName = "/$course"."_"."$dateForFile"."$extensionOfFile "; // name of the file
if(!empty($_POST['barcode']))
{
$barcode = $_POST['barcode']; // getting barcode
$time = time(); // unix time stamp
$actual_time = date('H:i:s',$time); // actual time
$file = $filePath.$fileName;
$entry = $barcode .", ".date ("d-m-Y"). " ".$actual_time; // putting in the data in variable
$handle = fopen("$file",'a'); // open a file
fwrite($handle,$entry."\r\n"); // "/r/n" will give a line break
fclose($handle); // free the file
//echo date_default_timezone_get(); // if required to check the timezone of the server
$countExisting = count(explode("\r\n", file_get_contents($file))) - 1; // minus 1 as every row has \r\n appended
$counter = $countExisting;
}
echo $counter;
header("location :barcodeHandler.php"); // redirect to the form
?>
这应该做你想要的。