Who's Online

มี 142 ผู้มาเยือน และ ไม่มีสมาชิกออนไลน์ ออนไลน์

การสร้างระบบงาน Chat Room อย่างง่าย ๆ

ดาวไม่ได้ใช้งานดาวไม่ได้ใช้งานดาวไม่ได้ใช้งานดาวไม่ได้ใช้งานดาวไม่ได้ใช้งาน
 

การสร้างระบบงาน Chat Room อย่างง่าย ๆ

เริ่มด้วยสร้าง ฐานข้อมูลไว้ชื่อ datatest และ table ชื่อ chatroom

CREATE TABLE `chatroom` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT 
, `mes` TINYTEXT NOT NULL , PRIMARY KEY ( `id` ));

สร้างไฟล์ชื่อ chatroom.php ตาม code ด้านล่างนี้

<?
if ($_POST[mes] <>"")
{
$hostname="localhost";
$user="";
$password="";
$dbname="datatest";
$connect=mysql_connect($hostname,$user,$password) or die("cannot connect");
$db=mysql_select_db($dbname)or die("cannot connect database!");
$sql="INSERT INTO chatroom ( mes)VALUES ('$_POST[mes]');";
$dbquery=mysql_db_query($dbname,$sql);
}
?>
<html >
<head>
<META HTTP-EQUIV="REFRESH" CONTENT="5">
 
<? //<META HTTP-EQUIV="REFRESH" CONTENT="5">REFRESH ทุก 5 วินาที ?>
<title>Untitled Document</title>
</head>

<body>
<table width="70%" border="0" cellspacing="0" cellpadding="0">
<tr>
<th bgcolor="f2f2f2" scope="row"><form name="form2" method="post" action="<?=$PHP_SELF;?>">
<label>
<div align="left">
<?
$hostname="localhost";
$user="";
$password="";
$dbname="datatest";//ชื่อฐานข้อมูล
$connect=mysql_connect($hostname,$user,$password) or die("cannot connect");
$db=mysql_select_db($dbname)or die("cannot connect database!");
$sql="select * from chatroom order by id DESC limit 0,10";//เรียกข้อมูล
$dbquery=mysql_db_query($dbname,$sql);
$row=mysql_num_rows($dbquery);
while($result=mysql_fetch_array($dbquery)) {
$a[] = $result[mes]; //เก็บช้อความ 10 อันดับล่าสุดลงตัวแปร 
}

for($i=count($a)-1;$i>=0;$i--) {
echo $a[$i]."<br>";
}
?>
</div>
</label>
</form> </th>
</tr>
<tr>
<th scope="row"><form name="form1" method="post" action="">
<table width="88%" border="1" cellspacing="0" cellpadding="0">
<tr>
<th scope="row"><font size="2">พิมพ์ข้อความที่นี่</font></th>
<td><label>
<div align="center">
<input name="mes" type="text" id="mes" size="60">
</div>
</label></td>
<td><label>
<input name="Submit" type="submit" id="Submit" value="Send">
</label></td>
</tr>
</table>
</form> </th>
</tr>
</table>
</body>
</html>

ผลที่ได้คือ

** นี่เป็นแค่หลักการง่าย ๆ ครับถ้าเอาไปใช้จริงล่ะก็ต้องลองพัฒนาเองกันดูนะครับ

About I-Comz

bcomz.com เป็นส่วนหนึ่งของงานซ่อมคอมพิวเตอร์นอกสถานที่ บางกะปิ รามคำแหง ซึ่งมีประสบการณ์การซ่อมและการค้าขาย มากกว่า 10 ปี โดยมอบประสบการณ์การช้อปปิ้งและช่องทางการขายปลีกที่สะดวกสบายสำหรับผู้บริโภคและมอบแพลตฟอร์มสำหรับผู้ค้าให้สามารถเข้าถึงฐานลูกค้าที่ใหญ่ที่สุดในเอเชียตะวันออกเฉียงใต้ได้อย่างง่ายดาย

Counter

เนื้อหาที่เปิดอ่าน
116363
©2024 ซ่อมคอมพิวเตอร์นอกสถานที่ All Rights Reserved. Designed By Becomz

Search