この記事ではPHPを始めたばかりの人向けに解説しています。
今回は掲示板を作成します。
変数の使い方や、簡単な判定文や繰り返し文を理解していることを前提としています。
本当に始めたばかりの人には少しハードルが高いかもしれませんが、たくさんの重要な要素を学習することができます。
これを覚えたらいろいろ応用もできるので、是非試してみてください。
今回は詳細な設計は省きますが、簡単にどんな機能のものを作るか決めておきます。
今回はスレッド型にします。
話題毎にスレッドがあり、それに対してレス(レスポンスの略)が行われる形式です。
とりあえず以下のような機能とします。
トップ画面で、スレッド名を作成日の降順に一覧表示。
スレッド名をクリックすると、スレッド画面へ遷移。
スレッド画面では、タイトル、説明、日付を表示して、レスを書込日時の降順で一覧表示。
スレッドとレスは誰でも作成できる。
以下の画面が必要になります。
トップ画面(スレッド一覧画面)
各スレッド画面
スレッド作成画面
レス投稿画面
編集や削除などはなく、一番シンプルなものを作ってみます。
スレッドとレスのデータを保存するために以下のSQLで2つのテーブルを生成します。
CREATE TABLE IF NOT EXISTS `threads` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`body` text NOT NULL,
`created_at` DATETIME NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `responses` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`thread_id` INT NOT NULL,
`body` text NOT NULL,
`name` VARCHAR(64) NOT NULL,
`created_at` DATETIME NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
※下記で紹介するソースコードは実際に画面で確認していないため、間違いがあったらごめんなさい。
トップ画面(index.php)
<?php
//DB接続
$dbh=mysql_connect ("hostname", "username", "passwork") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("dbname");
//スレッドを取得
$sql = "SELECT * FROM threads order by created_at desc";
$result = mysql_query($sql);
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>トップ</title>
</head>
<body>
<p><a href="thread_new.php">スレッド作成</a></p>
<table>
<?php while($thread = mysql_fetch_array($result)):?>
<tr><td><a href="thread.php?id=<?php echo $thread['id'];?>"><?php echo $thread['title'];?></a></td><td><?php echo $thread['created_at'];?></td></tr>
<?php endwhile;?>
</table>
</body>
</html>
各スレッド画面(thread.php)
<?php
//DB接続
$dbh=mysql_connect ("hostname", "username", "passwork") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("dbname");
//スレッドIDを取得
$id = $_GET['id'];
//スレッドを取得
$sql_thread = "SELECT * FROM threads where id = " . $id;
$result_thread = mysql_query($sql_thread);
$thread = mysql_fetch_array($result_thread);
//レスを取得
$sql_res = "SELECT * FROM responses where thread_id = " . $id . " order by created_at desc";
$result_res = mysql_query($sql_res);
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><?php echo $thread['title'];?></title>
</head>
<body>
<p>作成日時:<?php echo $thread['created_at'];?></p>
<p>タイトル:<?php echo $thread['title'];?></p>
<p><?php echo $thread['body'];?></p>
<p><a href="res_new.php?id=<?php echo $id;>">書き込み</a></p>
<?php while($res = mysql_fetch_array($result_res)):?>
<hr />
<p><?php echo $res['body'];?></p>
<p>名前:<?php echo $res['name'];?></p>
<p>投稿日時:<?php echo $res['created_at'];?></p>
<?php endwhile;?>
</body>
</html>
スレッド作成画面(thread_new.php)
<?php
$type = (isset($_POST['type']))? $_POST['type'] : null;
if($type=='create') {
//DB接続
$dbh=mysql_connect ("hostname", "username", "passwork") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("dbname");
//書き込み
$sql_thread = "update threads set title = '" . $_POST['title'] . "', body = '" . $_POST['body'] . "', created_at = now()";
$result_thread = mysql_query($sql_thread);
//スレッド画面に遷移
header("Location: index.php");
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>スレッド作成画面</title>
</head>
<body>
<form method="post" action="thread_new.php">
<table>
<tr>
<th>タイトル</th>
<td><input type="text" name="title" /></td>
</tr>
<tr>
<th>内容</th>
<td><textarea name="body"></textarea></td>
</tr>
<tr>
<td><input type="hidden" name="type" value="create" /></td>
<td><input type="submit" name="submit" value="作成" /></td>
</tr>
</table>
</form>
</body>
</html>
レス投稿画面(res_new.php)
<?php
$id = (isset($_GET['id']))? $_GET['id'] : null;
$type = (isset($_POST['type']))? $_POST['type'] : null;
if($type=='create') {
$id = $_POST['id'];
//DB接続
$dbh=mysql_connect ("hostname", "username", "passwork") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("dbname");
//書き込み
$sql_res = "update responses set thread_id = " . $id . " name = '" . $_POST['name'] . "', body = '" . $_POST['body'] . "', created_at = now()";
$result_res = mysql_query($sql_res);
//スレッド画面に遷移
header("Location: thread.php?id=" . $id);
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>レス投稿画面</title>
</head>
<body>
<form method="post" action="res_new.php">
<table>
<tr>
<th>名前</th>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<th>内容</th>
<td><textarea name="body"></textarea></td>
</tr>
<tr>
<td>
<input type="hidden" name="id" value="<?php echo $id;>" />
<input type="hidden" name="type" value="create" />
</td>
<td><input type="submit" name="submit" value="投稿" /></td>
</tr>
</table>
</form>
</body>
</html>
今回は入門ということで、基礎的な処理についてのみ紹介しました。
実際には、DB書き込み時にデータをエスケープしたり、表示時には特殊文字をHTMLエンティティに変換したりすることが必要です。
これらのことを意識して作成できたなら、次は編集・削除機能を付けたり、投稿内容の入力チェックや書込みや投稿後にメッセージを表示するなど、いろいろカスタマイズしてみてください。