国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

用PHP來(lái)制作評(píng)論系統(tǒng)

2024-05-04 23:05:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
  有什么問(wèn)題請(qǐng)與我聯(lián)系:http://www.survivalescaperooms.com [email protected]


  我們?cè)趯?shí)際做的過(guò)程中很是簡(jiǎn)單的,希望大家好好研究一下,給補(bǔ)充多點(diǎn)功能。本程序須在php and mysql的環(huán)境下運(yùn)行。有三個(gè)文件:comments.php, 是用來(lái)顯示[評(píng)論的, commentadd.php, 用來(lái)處理評(píng)論內(nèi)容的, and commentform.html 通過(guò)from來(lái)提交評(píng)論。

  1.首先建立一個(gè)數(shù)據(jù)庫(kù),如果已經(jīng)建立則建立一個(gè)符合條件的表:
  create table `comtbl` (
  `postid` int not null auto_increment ,
  `posttitle` text not null ,
  `postername` text not null ,
  `posteremail` text not null ,
  `posttime` timestamp not null ,
  `posttxt` text not null ,
  primary key ( `postid` )
  );

  評(píng)論查看頁(yè):comments.php,具體內(nèi)容為(有用戶名和密碼的在實(shí)際工作中要改變):
$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("comments");
接下來(lái)需要對(duì)表進(jìn)行查詢,并且把id 按descending: 順序排序:

  $result = mysql_query("select * from comtbl order by postid desc");
if (!$result) {
echo("<b>error performing query: " . mysql_error() . "</b>");
exit();
}

  在這里因?yàn)橐x出好多條記錄,所以用循環(huán)來(lái)讀,具體程序如下:
while ($row = mysql_fetch_array($result) ) {
$msgtxt = $row["posttxt"];
$msgid = $row["postid"];
$signame = $row["postername"];
$sigdate = $row["posttime"];
$msgtitle = $row["posttitle"];
$url = $row["posteremail"];

  現(xiàn)在到了最關(guān)鍵的一步了,也是困難的一步: 因?yàn)樵谶@里用到mysql's timestamp 函數(shù) (功能是可以自動(dòng)的餓把時(shí)間添加到一個(gè)表中),并且需要取得時(shí)間的字符串,使用字符串函數(shù)substr() ( $yr 表示年, $mo 表示月, 等等):

$yr = substr($sigdate, 2, 2);
$mo = substr($sigdate, 4, 2);
$da = substr($sigdate, 6, 2);
$hr = substr($sigdate, 8, 2);
$min = substr($sigdate, 10, 2);
  還需要對(duì)上述代碼的功能加以擴(kuò)充來(lái)實(shí)現(xiàn)12或24小時(shí)表示或者用 am和pm來(lái)表示上下午,代碼如下:

  if ($hr > "11") {
$x = "12";
$timetype = "pm";
$hr = $hr - 12;
}else{
$timetype = "am";
}

  另外,當(dāng)評(píng)論者要是留下email 的話,我們可以在這里來(lái)建立一個(gè)連接實(shí)現(xiàn)聯(lián)系發(fā)評(píng)論的人.代碼如下:

  if (!$url) {
$url = "#";
}else{
$stat = $url;
$url = "mailto:" . $url . "";
}

  最后,我們可以按行來(lái)顯示數(shù)據(jù),并且關(guān)閉循環(huán),最終的顯示代碼如下:

  echo("<p><b>$msgtitle</b>
  $msgtxt<br>
  <div align=right>$hr:$min $timetype | $mo/$da/$yr | $msgid, <a href='$url'>$signame</a></div></p>");

}
  <p><b>message title</b>
  text within the message, blah blah<br>
  <div align=right>hour:minute am/pm | month/day/year | message id, <a href='mailto:[email protected]'>name with email link</a></div></p>

  表單處理的程序: commentadd.php

  首先我們?cè)O(shè)置一些變量,然后通過(guò)表單把變量獲得的數(shù)據(jù)提交到后臺(tái)數(shù)據(jù)庫(kù)中,并且請(qǐng)記住用戶名和密碼。

  $assume = $_post['assume'];
  $posteremail = $_post['postemail'];
  $posttxt = $_post['posttxt'];
  $postername = $_post['poster'];
  $posttitle = $_post['posttitle'];

if ($assume == "true") {

$dbcnx = mysql_connect("localhost", "username", "password");

mysql_select_db("comments");

$sql = "insert into comtbl set postername='$postername', posteremail='$posteremail',
posttxt='$posttxt', posttitle='$posttitle'";

if (mysql_query($sql)) {
echo("<p>your comment has been added</p>");

} else {
echo("<p>error adding entry: " . mysql_error() . "</p>");
}
}

  提交了自己的評(píng)論之后還要有跳轉(zhuǎn)的功能,下面的javascript代碼就可以實(shí)現(xiàn)跳轉(zhuǎn)到指定的頁(yè)面。

<script language=javascript>
<!--
location.href="comments.php";
//-->
</script>

  下面是具體的commentform.html代碼,通過(guò)下面的內(nèi)容,可以讓發(fā)表評(píng)論者發(fā)表評(píng)論,然后通過(guò)提交可以把數(shù)據(jù)提交到commentadd.php里面來(lái)實(shí)現(xiàn)數(shù)據(jù)的在線提交。

<form action="commentadd.php" method=post>
<input type="text" name="poster" size="23" value="name"><br />
<input type="text" name="posttitle" size="23" value="name"><br />
<input type="text" name="postemail" size="23" value="[email protected]"><br />
<textarea cols=44 rows=6 name="posttxt" size=24 wrap="virtual">message<br />
<input type=hidden name=assume value=true>
<input type="submit" value="submit">

  下面是處理評(píng)論的代碼 comments.php:
<?
$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("comments");

$result = @mysql_query("select * from comtbl order by postid desc");
if (!$result) { echo("<b>error performing query: " . mysql_error() . "</b>");
exit();
}

while ($row = mysql_fetch_array($result) ) {
$msgtxt = $row["posttxt"];
$msgid = $row["postid"];
$signame = $row["postername"];
$sigdate = $row["posttime"];
$msgtitle = $row["posttitle"];
$url = $row["posteremail"];
$yr = substr($sigdate, 2, 2);
$mo = substr($sigdate, 4, 2);
$da = substr($sigdate, 6, 2);
$hr = substr($sigdate, 8, 2);
$min = substr($sigdate, 10, 2);

if ($hr > "11") {
$x = "12";
$timetype = "pm";
$hr = $hr - 12;
}else{
$timetype = "am";
}
if (!$url) {
$url = "#";
}else{
$stat = $url;
$url = "mailto:" . $url . "";
}

echo("<p><b>$msgtitle</b> $msgtxt<br><div align=right>
$hr:$min $timetype | $mo/$da/$yr | $msgid, <a href='$url'>$signame</a></div></p>");
}

?>
  下面是 commentadd.php:
<?
$assume = $_post['assume'];
$posteremail = $_post['postemail'];
$posttxt = $_post['posttxt'];
$postername = $_post['poster'];
$posttitle = $_post['posttitle'];

if ($assume == "true") {

$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("comments");
$sql = "insert into comtbl set postername='$postername', posteremail='$posteremail',
posttxt='$posttxt', posttitle='$posttitle'";
if (mysql_query($sql)) {
echo("your comment has been added");
} else {
echo("error adding entry: " . mysql_error() . "");
}
}

?>
<script language=javascript>
<!--
location.href="comments.php";
//-->
</script>

  整個(gè)程序到這就完了,想擁有自己評(píng)論系統(tǒng)的站長(zhǎng)請(qǐng)?jiān)囋嚢?script data-cfasync="false" src="/cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js">
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 台山市| 石楼县| 永善县| 长沙县| 措美县| 克山县| 苗栗市| 美姑县| 南充市| 彰化市| 湖口县| 九龙城区| 桃园县| 甘孜县| 怀化市| 三门峡市| 台北县| 武胜县| 台江县| 天门市| 威信县| 清水河县| 南溪县| 都江堰市| 临高县| 星子县| 湖州市| 松原市| 项城市| 镶黄旗| 安多县| 武清区| 博罗县| 阜宁县| 海城市| 晋江市| 宁都县| 军事| 丹凤县| 铅山县| 临潭县|