二連続投稿になりますが、頑張って行こうと思います。
それではいってみましょう!
ソースコード
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="" method="POST">
<p>名前 :<input type="text" name="name" value="名前"></p>
<p>コメント:<input type="text" name="comment" value="コメント"></p>
<p>削除番号:<input type="text" name="delete" value="number"> <input type="submit" value="送信"></p>
</form>
<hr>
<?php
$file_name = "mission3_3.txt";
$fp = fopen($file_name, "a");
$comments = file($file_name);
if(!empty($_POST["name"]) && !empty($_POST["comment"])) {
$order = count($comments) + 1;
$when = date('Y年m月d日 H時i分s秒')."にコメントを受け付けました<br/>";
$comme = $order."<>".$_POST["name"]."<>".$_POST["comment"]."<>".$when;
fwrite($fp, $comme.PHP_EOL);
}
elseif(!empty($_POST["delete"])) {
$comments = file($file_name, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
ftruncate($fp, 0);
fseek($fp, 0, SEEK_SET);
foreach($comments as $comme) {
$splits = explode("<>", $comme);
if($splits[0] != $_POST["delete"]) {
fwrite($fp, $comme.PHP_EOL);
}
}
}
$comments = file($file_name);
foreach($comments as $comment) {
$splits = explode("<>", $comment);
for($i = 0; $i < count($splits); $i++) {
echo $splits[$i]." ";
}
}
fclose($fp);
?>
</body>
</html>
実行結果
少しわかりにくいですが、実行結果を動画で掲載しておきます。
↓
コメント