2016-05-25 7 views
0

もし私がPHPで助けてくれるなら、私は本当に感謝しています。私はPHPには新しく、私はプロジェクトに取り組んでいます。テキストボックスに保存し、タイムスタンプ付きのテキストファイルに保存します。 これを行うことができ、コードが正しく機能しています。 しかし、いくつのエントリが入力されたか、エントリが重複している場合はカウンタを持つなど、いくつかの要件を満たすことができません。私は毎回ファイルのリロードとして私に1を表示するカウンタを含めるようにしてください。私は再度それを再読み込み中です、より多くのエントリを取得するために、再度テキストボックスが必要です。PHPどのくらいの腸が入力されたかをカウンター

<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

何をカウントしていますか? – Progrock

+0

年月日の形式のファイル名を使用し、ログ行の先頭に 'date( 'c'、$ timestamp)'を付けることをお勧めします。ソートやファイルやエントリの検索が容易になります。 – Progrock

+0

私のコードでは、テキストボックスに入力された値は "barcode10"です。これは変数$ barcodeに保存しています。それをtxtファイルに書き込みます。もう一度値を入力してください "バーコード10"はすでにスキャンされていますが、ファイルに書き込まれているが、私はバーコードが既にファイルにある場合、私はそれを捨ててテキストファイルに書き込まれません。 – Nav

答えて

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 
?> 

これは、必要な操作を行う必要があります。

+0

ありがとうたくさんのおかげであなたの助けをありがとう。それは正常に動作しています私は前にファイル内の文字列を数えようとしましたが、それは私にすべての時間を表示していた(実際には4行目の行が表示されます)なぜそういうのか分かりませんでした。何か明確なことがあるなら、私の混乱をクリアしてください。 – Nav

+0

とあなたがいくつかのsugestionを与えることができる場合はどうすれば私は倍数を処理することができます。私のコードでは、もし$ barcodeがalredyでスキャンされていれば、私はファイルに書き込んでいません。 – Nav

関連する問題