[php] ทำบอร์ดเองได้ง่ายๆ
posted on 11 Mar 2008 14:01 by rije in PHPเย้ๆๆ ใกล้จะจบแย้วงับ เอิ๊กๆๆ
-------------------------------------
วันนี้เอาโค้ด php มาแบ่งปันงับ
ทำบอร์ดได้ง่ายๆ ด้วยตัวเองน่อ!! ^[]^/ เริ่มกันเล้ย
-------------------------------------
เริ่มแรกสร้าง table ใน database ของคุณ
Table : board (เก็บกระทู้)
Table : reply (เก็บ reply)
ใน table ต้องมีอะไรบ้าง?
--------------------------------------
Table : board
===================
id , int(15) , auto_increment , PK
subject , varchar(200)
name , varchar(60)
email , varchar(100)
message , text
dates , timestamp , CURRENT_TIMESTAMP
--------------------------------------
Table : reply
===================
id_re , int(15) , auto_increment , PK
name , varchar(60)
email , varchar(100)
message , text
dates , timestamp , CURRENT_TIMESTAMP
id , int(15)
--------------------------------------
สร้าง page สำหรับเชื่อมต่อฐานข้อมูลดังนี้
--------------------------------------
page : connect.php
===================
<?
$host="localhost";
$user="root"; //ใส่ username สำหรับเชื่อมต่อเข้าเซิฟเวอร์ (เช่น phpmyadmin)
$pw="password"; //ใส่พลาส
$dbname="databasename"; //ใส่ชื่อดาต้าเบสที่จะใช้
$c = mysql_connect($host,$user,$pw);
if (!$c) {
echo "<h3>ERROR : Can not connect to database</h3>";
exit();
}
?>
-----------------------------------------
ต่อไปสร้างเพจแรกไว้โชว์หน้า webboard
-----------------------------------------
page : webboard.php
=====================
<html>
<head>
<title>your title</title>
<script>
function open_window(link,w,h)
{
var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=yes,scrollbars=yes";
newWin = window.open(link,'newWin',win);
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<table width=80% class="text"><tr><td colspan="2"><font size="+2">Webboard</font></td>
<td colspan="2" align="right"><a href="newtopic.php"><img src="images/newpost.gif" border=0></a></td></tr>
<tr>
<td align="center" width=10% class="textw" bgcolor="#3399CC"><b>id</b></td>
<td align="center" width=50% class="textw" bgcolor="#3399CC"><b>subject</b></td>
<td align="center" width=20% class="textw" bgcolor="#3399CC"><b>by</b></td>
<td align="center" width=20% class="textw" bgcolor="#3399CC"><b>date</b></td>
</tr>
<?
include "connect.php";
$count=1;
$sql="select * from board order by id desc ";
include "page.php"; //สำหรับโชว์เลขหน้า เดี๋ยวให้ไฟล์ไปครับ
while($record=mysql_fetch_array($result)) {
$count++;
echo '<tr>
<td align=center bgcolor="#e7e7e7">'.$record[0].'</td>
<td bgcolor="#e7e7e7"> <a href="javascript:open_window(\'view.php?id='.$record[0].'\',550,600);">'.$record[1].'</a></td>
<td align=center bgcolor="#e7e7e7"><a href="mailto:'.$record[3].'">'.$record[2].'</a></td>
<td align=center bgcolor="#e7e7e7"><font size="-2">'.$record[5].'</font></td>
</tr>';
}
?>
<tr><td align="right" colspan="4">
<? page_no(); ?>
</td></tr>
</table>
</body>
</html>
-----------------------------------------
เอาไปประยุกต์เองนะครับ ต่อไปเพจสำหรับสร้างกระทู้
-----------------------------------------
page : newtopic.php
====================
<html>
<head>
<title>your title</title>
<script>
function open_window(link,w,h)
{
var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=yes,scrollbars=yes";
newWin = window.open(link,'newWin',win);
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<?
include "connect.php";
if($_POST[subject]!=''){
$sql="INSERT INTO board (id, subject , name , email , message , dates) values ('','".$_POST["subject"]."','".$_POST[name]."','".$_POST["email"]."','".$_POST["message"]."',now())";
$result=mysql_db_query($dbname,$sql);
echo '
<font size="+2">ได้รับข้อความเรียบร้อยแล้ว</font><br>
<a href="webboard.php">คลิกที่นี่เพื่อกลับหน้าเว็บบอร์ด</a>
</body>
</html>';
exit();
}
?>
<!-- ส่วนใส่ code webboard -->
<table width=80% class="text"><tr><td align="center" height=50 valign="top"><font size="+2">Webboard : new post</font></td>
</tr>
<tr><td align="center"><form action="newtopic.php" method="post">
<table width="100%" class="textb">
<tr>
<td width=200 align="right">subject : </td>
<td><input type="text" name="subject" size="30"></td>
</tr>
<tr>
<td align="right">name : </td>
<td><input type="text" name="name" size="30"></td>
</tr>
<tr>
<td align="right">e-mail : </td>
<td><input type="text" name="email" size="30"></td>
</tr>
<tr>
<td align="right">message : </td>
<td><textarea name="message" cols="40" rows="5" id="textarea"></textarea></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" name="submit" value=" post "> <input type="reset" name="reset" value=" reset "></td>
</tr></table></form><p>
</td></tr>
</table>
</body>
</html>
-----------------------------------------
ขี้เกียจล่ะ เอาไฟล์ไปละกันครับ 555
page.php & reply.php & view.php : กดโหลดเล้ย!!
เพจอื่นก็ก๊อบ text ที่เขียนไว้เอานะครับ
ครั้งหน้าจะเอาส่วน admin มาแปะให้ครับ














มาบอกแบบนี้ก็ดีค่ะ ช่วยให้เข้าใจโค้ดขึ้นมากเลย
#1 By MiSa on 2008-03-11 17:25