2012-02-24 10 views
0

私は、チェックされたデータを含むphpファイルを持っています。訪問するたびに変数「$ visit」をインクリメントしてテキストファイルに保存するにはどうすればよいですか?テキストファイルからのデータの追加/インクリメント

new.php

<html> 
<body bgcolor="#99FF00"> 
<table border="1"> 
<FORM ACTION="new.php" METHOD="POST"> 
Enter maximum price <input type="text" name="maximumprice"/> 
<p><input type="submit" value="Go" name="Go"/> 
</form> 

<? 

$mPrice = $_POST['maximumprice']; 
$file1 = "properties.txt"; 
$filedata = fopen ($file1, "r"); 
$array1 = file ($file1); 

print "<form action=\"visit.php\" method=\"POST\">"; 




for ($counter1 = 0; $counter1 < count($array1); $counter1++) 
{ 
$arrLine = $array1[$counter1]; 

$pCode = getvalue ($arrLine, 0); 
$price = getvalue ($arrLine, 1); 
$picture = getvalue ($arrLine, 2); 
$visit = getvalue ($arrLine, 3); 



if ($price < $mPrice) 
{ 
print "<tr>"; 
print "<td>"; 

print $pCode. "<br>"; 
print $price. "<br>"; 
//print $picture. "<br>"; 
print $visit. "<br>"; 

print "<input type=\"checkbox\" name=\"box[]\" value=\"$arrLine\" />"; 

print "</td>"; 

print "<td>"; 
printf("<img src='$picture' width='200' height='150'>"); 
print "</td>"; 
print "</tr>"; 


} 
} 

print '<input type="submit" name="Visit" value="Visit"/>'; 

// Move the form outside the for loop 
print "</form>"; 


fclose ($filedata); 

function getvalue ($text, $arrNo) 
{ 
$intoarray = explode (",", $text); 
return $intoarray[$arrNo]; 
} 

?> 

</table> 
</body> 
</html> 

これは2ページ目で、display.php

<html> 
<body bgcolor="#99FF00"> 
<? 

foreach ($_POST['box'] as $values) 
{ 
echo "$values <hr/>"; 
} 



?> 
</body> 
</html> 
+0

私は他の誰も知らないが、これを数回読んで、あなたが求めていたものと達成しようとしているものをかなり得ることができなかった。あなたはもっと明確になりますか? – Paul

答えて

0

ステップ1:あなた以来、本当に

ありません:-)データベースを使用しますが、チェックボックスの値として行全体を保存している場合は、ファイル内の行と比較し、一致する行の訪問フィールドを更新できます。

たとえば、処理ファイルに:

$filename = "properties.txt"; 
$data  = file($filename, FILE_IGNORE_NEW_LINES); 

$checked = $_POST['box']; 

foreach($data as $id => $line){ 
    if(in_array($line, $checked)){ 
     //Explode the line into parts 
     $tmp = explode(',', $line); 
     //Increment the visit field 
     $tmp[3]++; 
     //Put the updated data back in the file data array 
     $data[$id] = implode(',', $tmp); 
     //Unset the tmp array 
     unset($tmp); 
    } 
} 

//implode the data back into a text block 
$data = implode("\n",$data); 
file_put_contents($filename, $data); 

これはテストされていないですが、あなたが...サイドノートとして

探しているものを得なければならない、あなたはfopen呼び出しを行う必要はありませんfile関数を使用します。ファイルを開いて読み込みます。

EDIT:訪問は各行の最後の列で、フラグがないと思われるので、file関数は各行の最後に改行を保持します。file関数に適切なフラグを追加しました。

関連する問題