ã“ã®è¨˜äº‹ã§ã¯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エンティティã«å¤‰æ›ã—ãŸã‚Šã™ã‚‹ã“ã¨ãŒå¿…è¦ã§ã™ã€‚
ã“れらã®ã“ã¨ã‚’æ„è˜ã—ã¦ä½œæˆã§ããŸãªã‚‰ã€æ¬¡ã¯ç·¨é›†ãƒ»å‰Šé™¤æ©Ÿèƒ½ã‚’付ã‘ãŸã‚Šã€æŠ•ç¨¿å†…容ã®å…¥åŠ›ãƒã‚§ãƒƒã‚¯ã‚„書込ã¿ã‚„投稿後ã«ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã‚’表示ã™ã‚‹ãªã©ã€ã„ã‚ã„ã‚カスタマイズã—ã¦ã¿ã¦ãã ã•ã„。