2012-02-23 33 views
0

我試圖從文本文件中用數組顯示一些數據。如何在數組中使用複選框

的複選框會出現,但,當用戶輸入搜索詞只有相關數據將拿出複選框,我的問題是我怎麼顯示已確認在不同的PHP文件中的數據?

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

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

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 "<form action=\"available.php\">"; 
print "$arrLine<input type=\"checkbox\" name=\"box[]\" value=\"$arrLine\" />"; 
print "</form>"; 
print "</td>"; 

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



} 
} 

fclose ($filedata); 

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

?> 

</table> 
</body> 
</html> 
+0

'檢查= 「檢查」',看到https://developer.mozilla.org/en/HTML/Element/input – hakre 2012-02-23 19:13:03

回答

1

如果你要允許多個複選框被選中,你需要把你的表單標籤外for循環。

這應該是你的代碼工作修改:

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

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

// Move the form outside the for loop 
print "<form action=\"available.php\" method=\"POST\">"; 

//SHOULD I DISPLAY SELECTED VALUES? 
if (isset($_POST['box'])) { 
    $array1 = $_POST['box']; 
    $mPrice = 10000000; // Since what I wanna see is already selected, set maxprice to ALOT. 

} else { // read from file 
// $mPrice = $_POST['maximumprice']; 
// $file1 = "properties.txt"; 
// $filedata = fopen ($file1, "r"); 
// $array1 = file ($file1); 
} 

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 "$arrLine<input type=\"checkbox\" name=\"box[]\" value=\"$arrLine\" />"; 

print "</td>"; 

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


} 
} 
// Add a view selected button 
print '<input type="submit" name="selectedList" value="View selected"/>'; 

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


fclose ($filedata); 

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

?> 

</table> 
</body> 
</html> 
+0

請注意即使我只使用發佈的數據來呈現表,我也打開了properties.txt文件。你不會在生產環境中這樣做。 – 2012-02-23 20:05:26

+0

我其實想要在另一個.php文件上顯示,但你確實指出了我的錯誤,在for循環中有了這個窗體。 – 2012-02-24 19:11:32

2

下面的代碼修改您的需求。這是更簡單的方式來顯示index.phpshow.php中選擇的複選框。本例中我選擇了一個POST方法。

的index.php

echo "<form method='post' action='show.php'>"; 
echo "<input type='checkbox' name='box[]' value='$arrLine' /> $arrLine"; 
echo "</form>" 

show.php

$options = $_POST['box']; 
foreach($options as $option) { 
    echo "<p>$option<p>"; 
} 
相關問題