다음 형식의 텍스트 파일이 있습니다. stats.txt라고합니다.
valueA1, valueA2, valueA3, valueA4
valueB1, valueB2, valueB3, valueB4
valueC1, valueC2, valueC3, valueC4
valueD1, valueD2, valueD3, valueD4
4 개의 텍스트 상자 또는 텍스트 영역을 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>
누군가 제발 도와 줄 수 있습니까?
줄을 반복하고 텍스트 영역을 표시하십시오. file ()을 사용하여 파일을 배열에 넣습니다.
<?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);
};
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다