基本上下面的代码在文本文件中读取,并在屏幕上播放它,每行附近都有复选框。但是现在我希望用户能够选中任何框,然后在新的 PHP 文件中显示所选结果 - 我以为我必须再次阅读文本文件并以某种方式将其引用到数组,但我仍然卡住了,所以任何帮助将不胜感激。
谢谢。
第一个 php 文件
<?php
$filename = "file.txt";
$myarray = file($filename);
print "<form action='file2.php' method='post'>'n";
// get number of elements in array with count
$count = 0; // Foreach with counter is probably best here
foreach ($myarray as $line) {
$count++; // increment the counter
$par = getvalue($line);
if ($par[1] <= 200) {
// Note the [] after the input name
print "<input type='checkbox' name='test[$count]' /> ";
print $par[0]." ";
print $par[1]." ";
print $par[2]." ";
print $par[3]."<br />'n";
}
}
print "</form>";
第二个 php 文件,应显示所选结果
<?php
$filename = "file.txt";
$myarray = file($filename);
?>
我认为你把问题复杂化了。您可以只给复选框一个value
的致敬,然后从第二页读取数组。从第二页上的 kus print_r ($_POST)
开始,以帮助您了解必须使用的内容。
1)想想你的文本文件的格式(可能是"Name1-Value 1-true'nName1-Value2-false"
)
2)学习这个函数
3) 使用默认选项
创建一个文件4)制作一个PHP脚本来打开文件,创建一个数组并打印resoult - 例如:
$handle = fopen('./file.txt','r');
$fileString = fread($handle,filesize('./file.txt'));
$handle = null; //Similar to unset($handle);
$array = explode($fileString, "'n");
echo '<form action="./script2.php">';
foreach ($array as $value) {
$value = explode($value, "-");
echo '<input type="checkbox" name="'.$value[1].'" value="'.$value[2].'" checked="';
if ($value[3]==true) {
echo 'checked" /><br />';
} else {
echo '" /><br />';
}
}
5) 创建编辑文件的第二个脚本 - 例如:
if ($_GET == null;) {exit("He's dead, Jim!");}
$handle = fopen('./file.txt','r');
$fileString = fread($handle,filesize('./file.txt'));
//Do something with the string :D
$handle = fopen('./file.txt','w');
fwrite($handle,$fileString);