整体代码展示
1,目录结构
2,整体代码展示:
conn.inc.php:
<?php define("HOST",'localhost'); define("USER",'root'); define("PWD",'root'); define("DBNAME",'onecms');
insertdb.php:
<?php include './mysqli.php'; $tit=$_POST["title"]; $con=$_POST["content"]; $sql="insert into message(title,content) values('$tit','$con')"; if($mysqli->query($sql)) { echo 1; } else{ echo 0; }
message.php:
<!DOCTYPE html> <html> <head> <title>留言板</title> <meta charset="UTF-8"> <script src="jquery-1.11.0.js"></script> <script src="kindeditor/kindeditor/kindeditor-all.js"></script> <style> #div1 div{height: 30px; line-height: 30px; padding-left: 10px; background: #f0f0f0; margin-bottom: 1px} </style> <script> var page=1; var num=8; // var str=""; function load(page){ function showpage() { //获取总页数 $.ajax({ async:false,//设置成同步 函数内部变量 外部可以取到 type:'get', url:'totaldb.php', data:{page:page,num:num}, dataType:'text', success: function(data,status) { totalpage=data; } }) var result=""; //显示分页 result="<input type='button' id='prev' value='上一页'></input>"; result+='当前页:第'+page+'页/总共:'+totalpage+"页"; result+="<input type='button' id='next'value='下一页'></input>"; $('#page').html(result); //单击下一页 page递增 实现翻页 $('#next').click(function(){ page++; if(page>=totalpage) { page=totalpage; } load(page); }) //单击上一页 page递减 实现翻页 $('#prev').click(function(){ page--; if(page<=1) { page=1; } load(page); }) } //获取内容 var str=""; $.ajax({ type:'get', url:'messageshowdb.php', data:{page:page,num:num}, dataType:'json', success: function(data,status) { //{ // "1":{"title":"t1","content":"c1"}, // "2":{"title":"t2","content":"c2"}, // "3":{"title":"t3","content":"c3"}, //} str=""; $.each(data, function(key,value) { str+="<div>"+[key]+":"+"标题:"+value.title+"-----"+"内容:"+value.content+"</div>"; $("#div1").html(str); }); } }) showpage(); } //加载方法 $(function(){ load(page); $("#btn").click(function () { var title=$("#title").val(); var content=$("#content").val(); $.post("insertdb.php",{title:title,content:content},function (data) { if(data){ alert("留言成功!"); }else{ alert("留言失败请重新输入!"); } load(page); location.href="message.php"; },"text") }) $("#sousuo").click(function () { str=""; var con=$("#con").val(); $.get("messageshowdb.php?content=sousuo",{con:con,page:page,num:num},function (data) { $.each(data, function(key,value) { str+="<div>"+[key]+":"+"标题:"+value.title+"-----"+"内容:"+value.content+"</div>"; $("#div1").html(str); }); },"json") showpage(); }) }) </script> </head> <body> <div><h1>留言板</h1></div> <div>搜索:<input id="con" name="sousuo"><input id="sousuo" type="button" value="确定"></div> <br><div id="div1"></div> <div id="page"></div> <div> 标题:<input type="text" id="title" name="title"><br> 内容:<br><span><textarea name="content" rows="13" cols="80" id="content"></textarea> <script> KindEditor.ready(function(K) { window.editor = K.create('#content',{ afterBlur:function(){this.sync();} }) }); </script> </span> <input type="submit" name="dosub" id="btn" value="上传留言"> </div> </body> </html>
messageshowdb.php:
<?php include 'mysqli.php'; $page= isset($_GET["page"])?$_GET["page"]:1; $num=isset($_GET["num"])?$_GET["num"]:4; $startnum=($page-1)*$num; //开始位置 $con=isset($_GET["con"])?$_GET["con"]:"";//搜索关键字 $content=isset($_GET["content"])?$_GET["content"]:""; $sql="select * from message where 1=1"; if($content=="sousuo"){ $sql.=" and title like '%$con%' or content like '%$con%'"; } $sql.=" limit $startnum,$num"; $result=$mysqli->query($sql); if($result->num_rows>0) { while ($row=$result->fetch_assoc()) { $arr[$row["id"]]["title"]=$row["title"];//$arr[1]["title"]=$row["title"] $arr[$row["id"]]["content"]=$row["content"];//$arr[1]["content"]=$arr["content"] } } echo json_encode($arr);
mysqli.php:
<?php include 'conn.inc.php'; $mysqli=new mysqli(HOST,USER,PWD,DBNAME); if($mysqli->connect_errno){ die('数据库链接出错'.$mysqli->connect_error); }
totaldb.php:
<?php include './mysqli.php'; $num=$_GET["num"];//每页显示的个数 $sql="select * from message"; $result=$mysqli->query($sql); $totalnum=$result->num_rows;//总记录数 $totalpage=ceil($totalnum/$num); echo $totalpage;