Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何对数组使用复选框_Php_Arrays_Checkbox - Fatal编程技术网

Php 如何对数组使用复选框

Php 如何对数组使用复选框,php,arrays,checkbox,Php,Arrays,Checkbox,我试图用数组显示文本文件中的一些数据 复选框会出现,但当用户输入搜索词时,只有相关数据才会出现复选框,我的问题是如何显示已在不同PHP文件上选中的数据 <html> <body bgcolor="#99FF00"> <table border="1"> <FORM ACTION="available.php" METHOD="POST"> Enter maximum price <input type="text" name="maximu

我试图用数组显示文本文件中的一些数据

复选框会出现,但当用户输入搜索词时,只有相关数据才会出现复选框,我的问题是如何显示已在不同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>

输入最高价格



根据需要修改以下代码。这是在
show.php
中显示
index.php
页面中选择复选框的更简单方法。我为这个例子选择了
POST
方法

index.php

echo "<form method='post' action='show.php'>";
echo "<input type='checkbox' name='box[]' value='$arrLine' /> $arrLine";
echo "</form>"
$options = $_POST['box'];
foreach($options as $option) {
  echo "<p>$option<p>";
}
echo”“;
回声“$arrLine”;
回声“”
show.php

echo "<form method='post' action='show.php'>";
echo "<input type='checkbox' name='box[]' value='$arrLine' /> $arrLine";
echo "</form>"
$options = $_POST['box'];
foreach($options as $option) {
  echo "<p>$option<p>";
}
$options=$\u POST['box'];
foreach($options作为$option){
echo“$option”;
}

根据需要修改以下代码。这是在
show.php
中显示
index.php
页面中选择复选框的更简单方法。我为这个例子选择了
POST
方法

index.php

echo "<form method='post' action='show.php'>";
echo "<input type='checkbox' name='box[]' value='$arrLine' /> $arrLine";
echo "</form>"
$options = $_POST['box'];
foreach($options as $option) {
  echo "<p>$option<p>";
}
echo”“;
回声“$arrLine”;
回声“”
show.php

echo "<form method='post' action='show.php'>";
echo "<input type='checkbox' name='box[]' value='$arrLine' /> $arrLine";
echo "</form>"
$options = $_POST['box'];
foreach($options as $option) {
  echo "<p>$option<p>";
}
$options=$\u POST['box'];
foreach($options作为$option){
echo“$option”;
}

如果要允许选中多个复选框,则需要将表单标记置于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>

输入最高价格



如果要允许选中多个复选框,则需要将表单标记置于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>

输入最高价格



checked=“checked”
,请参见
checked=“checked”
,请注意,我打开了properties.txt文件,即使我只使用发布的数据来显示表格。您不会在生产环境中这样做。我实际上希望它显示在另一个.php文件中,但您确实指出了我的错误,表单位于for循环中。请注意,我打开了properties.txt文件,尽管我只使用发布的数据来表示表。在生产环境中是不会这样做的,我实际上想在另一个.php文件中显示它,但您确实指出了我的错误,将表单放在for循环中。