2012-06-30 18 views
1

我有一个PHP文件,从一个txt文件中的信息读取和打印在屏幕上成线,如使用复选框在PHP试图读取信息

第一线[X]
第二线[X] 等等等等等

我想添加复选框旁边的所有信息行,我设法做一个循环,创建复选框取决于多少行被读取。

现在我坚持的最后一件事是,我希望用户能够点击任何复选框,然后单击提交按钮,应该在新的php文件上打印出所选信息。

如果用户选中第1行和提交,那么它应该显示在开幕PHP文件

我做了一些研究,并成功地使用isset方法来找出它是否被选中的文本字符串“1号线”,这工作,但IM仍然不确定如何阅读这是检查到一个新的PHP文件中的信息任何帮助,将不胜感激谢谢

$filename = "file.txt"; 

$filepointer = fopen($filename, "r"); //open for read 

$myarray = file ($filename); 

// get number of elements in array with count 
for ($counts = 0; $counts < count($myarray); $counts++) 

{ //one line at a time 
$aline = $myarray[$counts]; 

//$par = array(); 
$par = getvalue($aline); 

if ($par[1] <= 200) 
{ 

print "<input type=checkbox name='test'/>"." ".$par[0]." "; 
print $par[1]." "; 
print $par[2]." "; 
print $par[3]." "; 

} 

} 

回答

2

我想你可能想创建,其识别线进行了检查数组?那么,你会想用一个数组来命名你的复选框输入。您可以使用与PHP非常相似的语法执行此操作,方法是将[]附加到输入名称。对于这种特定情况,您还需要显式索引数组键,您可以像[index]那样进行索引。这将是更容易在代码中证明这一点:

file1.php(FIXED):

<?php 

$filename = "file.txt"; 

// file() does not need a file pointer 
//$filepointer = fopen($filename, "r"); //open for read 

$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>"; 

file2.php:

<?php 

    foreach ($_POST['test'] as $lineno) { 
    print "Line $lineno was checked<br />\n"; 
    } 

编辑

说你想要file2.php显示被检查文件中的行:

<?php 

    $filename = "file.txt"; 

    $myarray = file($filename); 

    foreach ($_POST['test'] as $lineno) { 
    // We need to subtract 1 because arrays are indexed from 0 in PHP 
    print $myarray[$lineno - 1]; 
    } 
+0

thanx的帮助,但是当我运行php文件时,它只是打印出“Line on was checked”而不是打印文本文件中的信息 – Hashey100

+1

那么您需要将文件再次读入内存中' file2.php'。等一下,我会编辑。 – DaveRandom

+0

@ Hashey100查看上面编辑 – DaveRandom