Skip to content

Instantly share code, notes, and snippets.

@Kazunari-h
Created June 13, 2018 11:05
Show Gist options
  • Save Kazunari-h/3d6cf45adad00124634697ea08f69586 to your computer and use it in GitHub Desktop.
Save Kazunari-h/3d6cf45adad00124634697ea08f69586 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>名前を表示するよ</title>
<link rel="stylesheet" href="./style.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
</head>
<body>
<div class="wrapper">
<h1>投稿フォーム</h1>
<form
action="./sample26_bbs.php"
method="post"
name="form1">
<label for="nickname"> 名前: </label>
<input type="text" class="form-control form-control-lg" id="nickname" name="nickname">
<br>
<label for="message">メッセージ: </label>
<input type="text" class="form-control form-control-lg" id="message" name="msg">
<br>
<label for="message">画像URL: </label>
<input type="text" class="form-control form-control-lg" id="image" name="imgurl">
<br>
<input class="btn btn-success" type="submit" value="送信">
</form>
<header class="id8oV">
<div aria-label="ストーリーズを開く" class="aoVrC " role="button" tabindex="0">
<canvas width="170" height="170" style="transform: translate(-9px, -9px); position: absolute; top: 0px; left: 0px; width: 170px; height: 170px;"></canvas>
<div class="fZC9e" style="width: 152px; height: 152px;">
<img class="_7A2D8" src="http://placehold.jp/150x150.png" alt="">
</div>
</div>
<div class="f7QXd SM9CE">
<h1 class="TQQQl">
<a class="F2iT8" href="">
#アイスクリーム
</a>
</h1>
<span class="-nal3 ">投稿<span class="g47SY ">823,130</span>件</span>
</div>
</header>
<?php
if(empty($_POST)) {
// 空だった場合
$url = "";
$result = "";
} else {
// 何か値が送られていた場合
if(is_readable("./file/counter.txt")) {
$id =
(int)file_get_contents(
"./file/counter.txt") + 1;
}else {
$id = 1;
}
file_put_contents("./file/counter.txt", $id);
$url = $_POST["imgurl"];
$result = $id.",".$_POST["nickname"].",".$_POST["msg"].",".$url;
}
// ファイルの名前
$filename = "./file/db.txt";
// ファイルがあるかどうか
if (is_readable($filename)) {
// 読めた場合
if (isset($_POST["edit"])) {
// editという値が送られてきた場合
// 編集リクエスト
// idによって、編集内容を上書きする。
$posts = explode("\n", file_get_contents($filename));
for ($i=0; $i < count($posts); $i++) {
// post_idが含まれる場合 置換処理をしてあげる
$post_id = $_POST["post_id"];
$regex = preg_quote("/[0-9].+,.+?/", '/') ;
if(preg_match($regex, $posts[$i])) {
$posts[$i] = $result;
}
}
// 配列を改行コードでくっつける処理
$result = implode("\n", $posts);
}else {
// 新規書き込みリクエスト
$result = $result."\n".file_get_contents($filename);
}
}else {
// 読めなかった場合
print $filename."は読めませんでした。";
}
$list = explode("\n", $result);
for ($i=0; $i < count($list); $i++) {
$list2 = explode(",", $list[$i]);
$img = 'http://placehold.jp/300x300.png';
if ($i % 3 == 0) {
print "<div class='row'>";
}
print "<div class='content'><img src='$img' alt='img'></div>";
if ($i % 3 == 2) {
print "</div>";
}
}
// POSTで送った値がない場合、書き込みをしない。
if(!empty($_POST)) {
// 保存する処理
file_put_contents($filename, $result);
}
?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment