Как использовать флажки с массивом - PullRequest
0 голосов
/ 23 февраля 2012

Я пытаюсь отобразить некоторые данные из текстового файла с массивом.

Флажки отображаются, но, когда пользователь вводит поисковый запрос, в нем появляются только релевантные данные, у меня возникает вопрос, как отобразить данные, проверенные в другом файле 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>

Ответы [ 2 ]

2 голосов
/ 23 февраля 2012

Измените приведенный ниже код в соответствии с вашими потребностями.Это более простой способ показать выбранные флажки со страницы index.php в show.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>";
}
1 голос
/ 24 февраля 2012

Если вы хотите разрешить выбор нескольких флажков, вы должны поместить свой тег FORM вне цикла 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...