0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【PHP】PHP課題・名簿の作成

Last updated at Posted at 2021-05-31

image.png


<?php

echo $aa = $_SERVER['HTTP_HOST'];

echo $bb = $_SERVER['REQUEST_URI'];

$line = [];

define('FILE_PATH', './members.csv');

  if ($_SERVER['REQUEST_METHOD'] === 'POST'){

    if (isset($_POST['submit']) === TRUE) {
      $data[] = $_POST['id'];
      $data[] = $_POST['name'];
      $data[] = $_POST['address'];
      $data[] = $_POST['tel'];
    }

    $fp = fopen(FILE_PATH, 'a');

    if ($fp !== FALSE) {
      $line = implode(',' , $data);
      $result = fwrite($fp, $line . "\n");  

      fclose($fp);
    }
    //多重リロード防止
    header("Location:http://".$aa.$bb);

  }
   
 //ファイルが読み込み可能ならば、

  $fp = fopen(FILE_PATH, 'r');
  if ($fp !== FALSE) {

    $text = fgetcsv($fp);

    while ($text !== FALSE) {
      $lines[] = $text;
      $text = fgetcsv($fp);
    }
    fclose($fp);
  }
?>

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>名簿管理アプリ</title>
    <style>
      label {
        display:block;
        margin-bottom: 10px;
      }
      table {
        border-collapse: collapse;
      }
      th, td {
        border: solid 1px black;
        padding: 5px;
      }
  </style>
</head>
<body>
    <h1>名簿作成アプリ</h1>

    <form method="post">
        <label>ID:<input type="text" name="id" required></label>
        <label>名前:<input type="text" name="name" required></label>
        <label>住所:<input type="text" name="address" required></label>
        <label>電話番号:<input type="text" name="tel" required></label>
        <input type="submit" name="submit" value="送信">
        
    </form>
    
    <table>
      <thead>
      <?php  if(isset($lines)){ ?>
        <tr>
          <th>ID</th>
          <th>氏名</th>
          <th>住所</th>
          <th>電話番号</th>
        </tr>
        <?php } ?>
      </thead>
      <tbody>
        <?php 
            if(isset($lines)){
              foreach ($lines as $line) { ?>
                <tr>
                    <td><?php print $line[0]; ?></td>
                    <td><?php print $line[1]; ?></td>
                    <td><?php print $line[2]; ?></td>
                    <td><?php print $line[3]; ?></td>
                </tr>
          <?php } ?>
        <?php } ?>
      </tbody>
    </table>

</body>
</html>
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?