2012-02-24 32 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> 

這是第二頁,Display.php的

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

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



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

我不知道其他任何人,但我讀了幾次,不能完全明白你在問什麼,你想完成什麼。你能清楚嗎? – Paul 2012-02-24 19:39:35

回答

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函數。它會打開並讀取文件。

編輯:由於它看起來是訪問是每行中的最後一列,並沒有任何標誌,file函數將保持每行的結尾換行符,我添加適當的標誌file函數。