2014-11-06 56 views
0

我有以下格式的文本文件的不同的線路,讓我們stats.txt稱之爲:PHP代碼編輯一個文本文件

valueA1, valueA2, valueA3, valueA4 
valueB1, valueB2, valueB3, valueB4 
valueC1, valueC2, valueC3, valueC4 
valueD1, valueD2, valueD3, valueD4 

我想四個文本框或文本域與此代碼中填入一個html文檔和表單提交時,附加到文本文件的相應行。我不是很瞭解PHP,所以無法找到一種方法來做到這一點。

我當前的HTML文件看起來像這樣:

$url = 'editor.php'; 
$file = 'stats.txt'; 

// check if form has been submitted 
if (isset($_POST['text1'])) 
{ 
    // save the text contents 
    file_put_contents($file, $_POST['text1']); 

    // redirect to form again 
    header(sprintf('Location: %s', $url)); 
    printf('<a href="%s">Updated</a>.', htmlspecialchars($url)); 
    exit(); 
} 

// read the textfile 
$text = file_get_contents($file); 

?> 

我無法弄清楚文本文件的字裏行間如何循環,故有可怕的代碼這個塊來獲得每條線的值:

<?php 
    //for Line 1 
    $myLine1 = 1 ; 
    $linefile1 = new SplFileObject($file); 
    $linefile1->seek($myLine1-1); 

    //for Line 2 
    $myLine2 = 2 ; 
    $linefile2 = new SplFileObject($file); 
    $linefile2->seek($myLine2-1); 

    //for Line 3 
    $myLine3 = 3 ; 
    $linefile3 = new SplFileObject($file); 
    $linefile3->seek($myLine3-1); 

    //for Line 4 
    $myLine4 = 4 ; 
    $linefile4 = new SplFileObject($file); 
    $linefile4->seek($myLine4-1); 
    ?> 

而且形式如下:

<form action="" method="post"> 
#stuff 1 
<textarea id="stuff" name="stuff[]"><?php echo htmlspecialchars($linefile1) ?></textarea> 
#stuff 2 
<textarea id="stuff" name="stuff[]"><?php echo htmlspecialchars($linefile2) ?></textarea> 
#stuff 3 
<textarea id="stuff" name="stuff[]"><?php echo htmlspecialchars($linefile3) ?></textarea> 
#stuff 4 
<textarea id="stuff" name="stuff[]"><?php echo htmlspecialchars($linefile4) ?></textarea> 
<input type="submit" /> 
<input type="reset" /> 
</form> 

有人可以幫我個是嗎?

+0

你應該使用爆炸()把每個值到一個數組,因爲用逗號分隔。如果您可以控制它的存儲方式,最好使用serailize,這樣它就可以以數組形式存儲了。 – 2014-11-06 01:49:44

回答

0

只是循環。使用文件()把文件分成數組

<?php $file = file('stats.txt');?> 

    <form action="" method="post"> 
    <?php 
    foreach($file as $line) { 
     echo "<textarea id='stuff' name='stuff[]'>".htmlspecialchars($line)."</textarea>"; 
    };?> 
    <input type="submit" /> 
    <input type="reset" /> 
</form> 

然後當你提交表單

if(isset($_POST['stuff'])){ 
     $text = join("\n", $_POST['stuff']); 
     file_put_contents($file, $text); 
    }; 
+0

'foreach($ _ POST ['stuff'] as $ val){ $ text。= $ val。「\ n」;可以用'$ text = join(「\ n」,$ _POST ['stuff'])替換''但是,起初應該檢查它是否是數組。 – Cheery 2014-11-06 02:04:00

+0

Cheers @Cheery :) – KyleK 2014-11-06 02:04:43

+0

謝謝你們,這很棒。 – 2014-11-06 04:06:52

0

這裏是我寫的從文本文件存儲和檢索數據的代碼,你可以作爲一個例子使用:通過線和顯示文字區域

// Save Check Boxes in Serial Form for later retrieval 
$myFile = "equipment.txt"; 
if($save == "yes"){ 
$stringData = serialize(array($container_20,$container_40,$container_45,$container_RF,$container_HQ,$container_Chassis)); 
file_put_contents($myFile, $stringData); 
} 

// Read Saved File and Populate Check Boxes 
$recoveredData = file_get_contents($myFile); 
list($container_20,$container_40,$container_45,$container_RF,$container_HQ,$container_Chassis) = unserialize($recoveredData); 
+0

謝謝@ garey-hayes,會試試這個。 – 2014-11-06 04:07:39