本文實(shí)例為大家分享了PHP留言本功能的具體代碼,供大家參考,具體內(nèi)容如下
index.php
<?php error_reporting(0); //關(guān)閉NOTICE提示 require_once "conn.php"; $pagesize=5; //每頁(yè)顯示5條數(shù)據(jù) $sql="select count(*) from guestlist "; //選擇數(shù)據(jù)庫(kù),計(jì)算符合條件的行數(shù)并返回行數(shù) $result= mysql_query($sql); //執(zhí)行,如果成功則返回結(jié)果集(從數(shù)據(jù)庫(kù)中找到所有的數(shù)據(jù),返回條數(shù)) $row = mysql_fetch_row($result); //獲得數(shù)組 Array[0]="數(shù)據(jù)庫(kù)里的總條數(shù)" $infoCount =$row[0]; //獲得總條數(shù):取得數(shù)組中的值$row[0]="數(shù)據(jù)庫(kù)里的總條數(shù)" $pageCount = ceil($infoCount/$pagesize); //獲取總頁(yè)數(shù)(總個(gè)數(shù)/每頁(yè)的個(gè)數(shù)5) $currpage=empty ($_GET["page"])?1:$_GET["page"]; //如果當(dāng)前頁(yè)為空 則定義page=1即$currpage=1反之亦然 if($currpage>$pageCount) //如果輸入的頁(yè)數(shù)超過(guò)總頁(yè)數(shù)則默認(rèn)跳轉(zhuǎn)到最后一頁(yè) { $currpage=$pageCount; } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <!--此處添加了bootstrip樣式--> <link href="../dist/css/bootstrap.min.css" rel="external nofollow" type="text/css" rel="stylesheet" /> <link href="css/index.css" rel="external nofollow" type="text/css" rel="stylesheet" /> <script> function test(){ var sum; if(document.frm.title.value==''){ alert('請(qǐng)?zhí)顚?xiě)標(biāo)題'); return false; }else{ sum =document.frm.title.value.length; if(sum<5 || sum>20){ alert('標(biāo)題長(zhǎng)度 5-20個(gè)字符'); return false; } } if(document.frm.username.value==''){ alert('請(qǐng)?zhí)顚?xiě)用戶網(wǎng)名'); return false; } if(document.frm.content.value==''){ alert("請(qǐng)?zhí)顚?xiě)內(nèi)容"); return false; } return true; } </script> </head> <body> <div class="content"> <h5 style="color: red;"><?php echo $infoCount;?>條留言</h5><br/> <ul class="bt"> <li>留言標(biāo)題</li> <li>用戶網(wǎng)名</li> <li>時(shí)間</li> </ul> <?php //從當(dāng)前頁(yè)開(kāi)始 向下取出5個(gè) $re= mysql_query("select * from guestlist order by id desc limit ".($currpage-1)*$pagesize.",".$pagesize); while($row= mysql_fetch_assoc($re)) //得到一行數(shù)據(jù)的數(shù)組,再執(zhí)行則得到再下一行,如果得到是最后一行,那么再執(zhí)行則返回false { ?> <ul class="nr"> <li><?php echo $row["title"];?></li> <li><?php echo $row["username"];?></li> <li><?php echo $row["addtime"];?></li> </ul> <div class="lynr"> <p><strong>留言內(nèi)容:</strong></p><span><?php echo $row["content"];?></span> </div> <?php } ?> <hr style="width:800px"/> <ul class="pagination"> <!--上一頁(yè)--> <?php for($i=1;$i<=$pageCount;$i++) { if($i==$currpage) { echo "<li><a href=?page=".($i-1).">«</a></li>"; } } ?> <!--數(shù)字頁(yè)--> <?php for($i=1;$i<=$pageCount;$i++) { if($i==$currpage) { echo "<li ><a style='background-color:#EEEEEE'>$i</a></li>"; }else{ echo "<li><a href='?page=$i'>$i</a></li>";} } ?> <!--下一頁(yè)--> <?php for($i=1;$i<$pageCount;$i++) { if($i==$currpage) { echo "<li><a href=?page=".($i+1).">»</a></li>"; } } ?> </ul> <br/> <ul> </ul> <hr/> <strong style="color:red">發(fā)表留言</strong> <form action="result.php" method="post" name="frm" onsubmit="return test()"> <table cellpadding="0" cellspacing="0" > <tr> <td >留言標(biāo)題:</td> <td><input type="text" name="title" autocomplete="off"/></td> </tr> <tr> <td>網(wǎng)名:</td> <td><input type="text" name="username" autocomplete="off"/></td> </tr> <tr> <td>留言內(nèi)容:</td> <td><textarea name="content" cols="42" rows="5" autocomplete="off"/></textarea></td> </tr> <tr> <td></td> <td><input class="btn" type="submit" name="submit" value="提交"/></td> </tr> </table> </form> </div> </body> </html>
【網(wǎng)站聲明】本站除付費(fèi)源碼經(jīng)過(guò)測(cè)試外,其他素材未做測(cè)試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請(qǐng)勿用于商業(yè)用途。如損害你的權(quán)益請(qǐng)聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。