2012-06-30 17 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