anonymous No title
No License PHP
2021年05月25日
Copy Clone
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <title>簡易掲示板</title>
</head>
<body>
  <h1>簡易掲示板</h1>
  <form action="" method="POST">
    <p>名前 <br>
      <input type="text" name="name">
    </p>
    <p>コメント <br>
      <input type="text" name="comment">
    </p>
    <p><input type="submit" value="送信"></p>
  </form>
  <?php
  $filename = "kadai_2_4.txt";
  if (!empty($_POST["name"]) && isset($_POST["comment"])) {
    $name = $_POST["name"];
    $comment = $_POST["comment"];
    if (file_exists($filename)) {
      $num = count(file($filename)) + 1;
    } else {
      $num = 1;
    }
    $date = date("Y-m-d");
    $str = $num . "<>" . $name . "<>" . $comment . "<>" . $date . "\n";
    $fp = fopen($filename, "a");
    fwrite($fp, $str);
    fclose($fp);
  }
  ?>
  <h1>削除フォーム</h1>
  <form action="" method="POST">
    <p>削除番号 <br>
      <input type="number" name="delete">
    </p>
    <p><input type="submit" value="削除"></p>
  </form>
  <?php
  $filename = "kadai_2_4.txt";
  if (!empty($_POST["delete"])) {
    $delete = $_POST["delete"];
    $num = file($filename);
    $fp = fopen($filename, "w");
    foreach ($num as $line) {
      $lines = explode("<>", $line);
      if ($delete !== $line[0]) {
        fwrite($fp, $line);
      }
    }
    fclose($fp);
  }
  ?>
  <?php
  $filename = "kadai_2_4.txt";
  $ret_array = file("$filename");
  foreach ($ret_array as $value) {
    $texts = explode("<>", $value);
    echo $texts[0];
    echo $texts[1];
    echo $texts[2];
    echo $texts[3];
    echo "<br/>";
  }
  ?>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <title>簡易掲示板</title>
</head>
<body>
  <h1>簡易掲示板</h1>
  <form action="" method="POST">
    <p>名前 <br>
      <input type="text" name="name">
    </p>
    <p>コメント <br>
      <input type="text" name="comment">
    </p>
    <p><input type="submit" value="送信"></p>
  </form>
  <?php
  $filename = "kadai_2_4.txt";
  if (!empty($_POST["name"]) && isset($_POST["comment"])) {
    $name = $_POST["name"];
    $comment = $_POST["comment"];
    if (file_exists($filename)) {
      $num = count(file($filename)) + 1;
    } else {
      $num = 1;
    }
    $date = date("Y-m-d");
    $str = $num . "<>" . $name . "<>" . $comment . "<>" . $date . "\n";
    $fp = fopen($filename, "a");
    fwrite($fp, $str);
    fclose($fp);
  }
  ?>
  <h1>削除フォーム</h1>
  <form action="" method="POST">
    <p>削除番号 <br>
      <input type="number" name="delete">
    </p>
    <p><input type="submit" value="削除"></p>
  </form>
  <?php
  $filename = "kadai_2_4.txt";
  if (!empty($_POST["delete"])) {
    $delete = $_POST["delete"];
    $num = file($filename);
    $fp = fopen($filename, "w");
    foreach ($num as $line) {
      $lines = explode("<>", $line);
      if ($delete !== $line[0]) {
        fwrite($fp, $line);
      }
    }
    fclose($fp);
  }
  ?>
  <?php
  $filename = "kadai_2_4.txt";
  $ret_array = file("$filename");
  foreach ($ret_array as $value) {
    $texts = explode("<>", $value);
    echo $texts[0];
    echo $texts[1];
    echo $texts[2];
    echo $texts[3];
    echo "<br/>";
  }
  ?>
</body>
</html>
No one still commented. Please first comment.
Output