anonymous No title
No License PHP
2021年05月26日
Copy Clone
<?php
    $filename = "kadai_2_5.txt";
    $lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $editNumber = '';
    $editName = '';
    $editComment = '';
    if(isset($_POST["edit"])) {
        foreach($lines as $row) {
            $bbsRowData = explode("<>", $row);
            if($bbsRowData[0] == $_POST["number"]) {
                $editNumber = $bbsRowData[0];
                $editName = $bbsRowData[1];
                $editComment = $bbsRowData[2];
                break;
            }
        }
    }
    else if(isset($_POST["normal"])) {
        $now_date=date("Y-m-d");
        $writeData = ($_POST['edit_post'] ?: count($lines) + 1) . "<>" . $_POST['name'] . "<>" . $_POST['comment'] . "<>" . $now_date;
        if($_POST["edit_post"]) {
            foreach($lines as &$row) {
                $bbsRowData = explode("<>", $row);
                if($bbsRowData[0] == $_POST["edit_post"]) {
                    $row = $writeData;
                }
            }
        }
        else {
            $lines[] = $writeData;
        }
     file_put_contents($filename, implode("\n", $lines));
    }
?>
<!doctype html>
<html lang="ja">
 <head>
  <meta charset="UTF-8">
  <title>簡易掲示板</title>
 </head>
 <body>
  <h1>簡易掲示板</h1>
  <form action="kadai_2_5.php" method="POST">
   <input type="hidden" name="edit_post" value="<?php echo $editNumber; ?>">
   名前:<input type="text" name="name" value="<?php echo $editName; ?>">
   <br />
   コメント:<input name="comment" value="<?php echo $editComment; ?>">
   <br />
   <input type="submit" name="normal" value="送信">
  </form>
  <h1>編集用フォーム</h1>
  <form action="kadai_2_5.php" method="POST">
   編集番号:<input type="text" name="number">
   <input type="submit" name="edit" value="送信">
  </form>
  <?php
  $ret_array=file($filename);
  foreach ($ret_array as $value) {
    $file_array=explode("<>",$value);
    foreach ($file_array as $value_array) {
      echo($value_array."<br />\n");
    }
  }
   ?>
 </body>
</html>
<?php
    $filename = "kadai_2_5.txt";
    $lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $editNumber = '';
    $editName = '';
    $editComment = '';
    if(isset($_POST["edit"])) {
        foreach($lines as $row) {
            $bbsRowData = explode("<>", $row);
            if($bbsRowData[0] == $_POST["number"]) {
                $editNumber = $bbsRowData[0];
                $editName = $bbsRowData[1];
                $editComment = $bbsRowData[2];
                break;
            }
        }
    }
    else if(isset($_POST["normal"])) {
        $now_date=date("Y-m-d");
        $writeData = ($_POST['edit_post'] ?: count($lines) + 1) . "<>" . $_POST['name'] . "<>" . $_POST['comment'] . "<>" . $now_date;
        if($_POST["edit_post"]) {
            foreach($lines as &$row) {
                $bbsRowData = explode("<>", $row);
                if($bbsRowData[0] == $_POST["edit_post"]) {
                    $row = $writeData;
                }
            }
        }
        else {
            $lines[] = $writeData;
        }
     file_put_contents($filename, implode("\n", $lines));
    }
?>
<!doctype html>
<html lang="ja">
 <head>
  <meta charset="UTF-8">
  <title>簡易掲示板</title>
 </head>
 <body>
  <h1>簡易掲示板</h1>
  <form action="kadai_2_5.php" method="POST">
   <input type="hidden" name="edit_post" value="<?php echo $editNumber; ?>">
   名前:<input type="text" name="name" value="<?php echo $editName; ?>">
   <br />
   コメント:<input name="comment" value="<?php echo $editComment; ?>">
   <br />
   <input type="submit" name="normal" value="送信">
  </form>
  <h1>編集用フォーム</h1>
  <form action="kadai_2_5.php" method="POST">
   編集番号:<input type="text" name="number">
   <input type="submit" name="edit" value="送信">
  </form>
  <?php
  $ret_array=file($filename);
  foreach ($ret_array as $value) {
    $file_array=explode("<>",$value);
    foreach ($file_array as $value_array) {
      echo($value_array."<br />\n");
    }
  }
   ?>
 </body>
</html>
No one still commented. Please first comment.
Output