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

답변

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 함수에 적절한 플래그를 추가했습니다.