
<?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>