微博

来源:互联网 发布:nginx 安装后无法访问 编辑:程序博客网 时间:2024/06/10 08:37
weibo.php
<?php/** * Created by PhpStorm. * User: dllo * Date: 16/8/16 * Time: 上午10:41 */require_once "common.php";$sql = "SELECT * FROM weibo ORDER by id DESC LIMIT 5";$result = mysql_query($sql);?><!doctype html><html><head>    <meta charset="utf-8">    <title>新浪微博</title>    <style>        * { padding: 0; margin: 0; }        li { list-style: none; }        body { background: #f9f9f9; font-size: 14px; }        #box { width: 450px; padding: 10px; border: 1px solid #ccc; background: #f4f4f4; margin: 10px auto; }        #fill_in { margin-bottom: 10px; }        #fill_in li { padding: 5px 0; }        #fill_in .text { width: 380px; height: 30px; padding: 0 10px; border: 1px solid #ccc; line-height: 30px; font-size: 14px; font-family: arial; }        #fill_in textarea { width: 380px; height: 100px; line-height: 20px; padding: 5px 10px; border: 1px solid #ccc; font-size: 14px; font-family: arial; overflow: auto; vertical-align: top; }        #fill_in .btn { border: none; width: 100px; height: 30px; border: 1px solid #ccc; background: #fff; color: #666; font-size: 14px; position: relative; left: 42px; }        #message_text h3 { font-size: 14px; padding: 6px 0 4px 10px; background: #ddd; border-bottom: 1px solid #ccc; }        #message_text li { background: #f9f9f9; border-bottom: 1px solid #ccc; color: #666; overflow: hidden; }        .caozuo{ float:right;}        #message_text h3 { padding: 10px; font-size: 14px; line-height: 24px; }        #message_text p { padding: 0 10px 10px; text-indent: 28px; line-height: 20px; }        #page a{            font-size:24px;            margin-left: 20px;        }        #page a.active{            color:yellow;        }        #content{resize: none;outline: none}    </style></head><body><div id="box">    <ul id="fill_in">        <form>            <li>内容:<textarea id="content"></textarea></li>            <li><input id="btn" type="button" value="提交" class="btn" /></li>        </form>    </ul>    <div id="message_text">        <ul class="list">            <?php                while($row = mysql_fetch_assoc($result)){                    echo "<li data-id=".$row["id"].">";                    echo "<p>".$row["content"]."</p>";                    $time = date("Y年m月d日 H:i:s",$row["time"]);                    echo "<p class='caozuo'><span>{$time} 顶: <a href='javascript:void(0)' class='ding'>".$row["up"]."</a> 踩: <a href='javascript:void(0)' class='cai'>".$row["down"]."</a><a href='javascript:void(0)' class='remove'> 删除</a></span></p>";                    echo "</li>";                }            ?>        </ul>        <div id="page">            <?php                $sql="SELECT COUNT(id) FROM weibo";                $result = mysql_query($sql);                $row = mysql_fetch_row($result);                $pageCount = ceil($row[0]/5);                for ($i = 0;$i < $pageCount;$i++){?>                    <a href='javascript:void(0)' class=<?php if($i == 0){echo "active";} ?>><?php echo $i+1; ?></a>            <?php                }            ?>        </div>    </div></div><script src="jquery-2.1.3.min.js"></script><script>    $(function () {        $("#btn").on("click",function () {            $.ajax({                url:"weibo_api.php",                type:"get",                data:{                    type:"add",                    content:$("#content").val()                },                dataType:"json",                success:function (data) {                    if (data.err == 0){                        addLi($("#content").val(),data.time,0,0,data.id);                        $("#content").val("");                    }else{                        alert("提交失败");                    }                }            })        });       $(document).on("click",".ding",function (e) {            var _this = $(e.target);            $.ajax({                url:"weibo_api.php",                type:"get",                data:{                    type:"up",                    id:_this.parents("li").attr("data-id")                },                dataType:"json",                success:function (data) {                    if (data.err == 0){                        _this.html(data.upCount);                    }else{                        alert(data.msg);                    }                }            })        })        $(document).on("click",".cai",function (e) {            var _this = $(e.target);            $.ajax({                url:"weibo_api.php",                type:"get",                data:{                    type:"down",                    id:_this.parents("li").attr("data-id")                },                dataType:"json",                success:function (data) {                    if (data.err == 0){                        _this.html(data.downCount);                    }else{                        alert(data.msg);                    }                }            })        })        $(document).on("click",".remove",function (e) {            var _this = $(e.target);            $.ajax({                url:"weibo_api.php",                type:"get",                data:{                    type:"remove",                    id:_this.parents("li").attr("data-id"),                    lastId:_this.parents("ul").children().last().attr("data-id")                },                dataType:"json",                success:function (data) {                    if (data.err == 0){                        _this.parents("li").animate({                            height:0                        },function () {                            _this.parents("li").remove();                            if (data.result){                                var obj = data.result;                                addLi(obj.content,obj.time,obj.up,obj.down,obj.id,true);                            }                        })                    }else{                        alert(data.msg);                    }                }            })        })                    $("#page a").on("click",function () {            $("#page a").removeClass();            $(this).addClass("active");            $.ajax({                url:"weibo_api.php",                type:"get",                data:{                    type:"page",                    page:$(this).index()                },                dataType:"json",                success:function (data) {//                    console.log(data);                  if (data.err == 0){                      $("ul.list").empty();                      for(var i = 0;i < data.result.length;i++){                          var obj = data.result[i];                          addLi(obj.content,obj.time,obj.up,obj.down,obj.id,true);                      }                  }else{                      alert(data.msg);                  }                }            })        })        function add0(s) {            if (s < 10){                return "0"+s;            }else{                return s;            }        }                // 处理从后台返回的时间戳        function setTime(s) {            var data = new Date();            data.setTime(s*1000);            return data.getFullYear() + "年" + add0(data.getMonth()+1) + "月" +add0(data.getDate())+"日"+add0(data.getHours())+":"+add0(data.getMinutes())+":"+add0(data.getSeconds());        }        function addLi(content,time,up,down,id,isLast) {            var liObj = $("<li data-id="+id+"><p>"+content+"</p><p class='caozuo'><span>"+setTime(time)+" 顶: "+"<a href='javascript:void(0)' class='ding'>"+up+"</a>"+  " 踩: " +"<a href='javascript:void(0)'   class='cai'>"+down+"</a><a href='javascript:void(0)' class='remove'> 删除</a></span></p></li>");            // 用来判断加在第一行还是最后一行            if(isLast){                $(".list").append(liObj);            }else{                $(".list").prepend(liObj);            }            var height = liObj.height();            liObj.height(0);            liObj.animate({                height:height            })        }            })</script></body></html>
weibo_api.php
<?php/** * Created by PhpStorm. * User: dllo * Date: 16/8/16 * Time: 上午11:06 */require_once "common.php";// api:微博_api.php?type=xx&content=xx&id=xx// 请求方式: get// 参数: type: 类型(add,ding,cai)//      content: 添加数据的具体内容//      id : 顶或踩所在行的id// 返回值:{"err":0,"msg":"失败原因","time":14xxxx}$type = $_GET["type"];switch ($type){    // 添加内容    case "add":        $content = $_GET["content"];        $time = time();        $sql = "INSERT INTO weibo (id,content,time,up,down) VALUES (NULL,'{$content}','{$time}',0,0)";        mysql_query($sql);        if (mysql_insert_id() > 0){            $arr = array("id"=>mysql_insert_id(),"err"=>0,"msg"=>"成功","time"=>$time);            echo json_encode($arr);        }        break;    case "up":        $id = $_GET["id"];        $sql = "SELECT * FROM weibo WHERE id={$id}";        $result = mysql_query($sql);        $row = mysql_fetch_assoc($result);        $upCount = $row["up"];        $upCount++;        $sql = "UPDATE weibo SET up={$upCount} WHERE id={$id}";        mysql_query($sql);        if (mysql_affected_rows() > 0){            $arr = ["err"=>0,"msg"=>"更新成功","upCount"=>$upCount];            echo json_encode($arr);        }else{            echo '{"err":1,"msg":"更新失败"}';        }        break;    case "down":        $id = $_GET["id"];        $sql = "SELECT * FROM weibo WHERE id={$id}";        $result = mysql_query($sql);        $row = mysql_fetch_assoc($result);        $downCount = $row["down"];        $downCount++;        $sql = "UPDATE weibo SET down={$downCount} WHERE id={$id}";        mysql_query($sql);        if (mysql_affected_rows() > 0){            $arr = ["err"=>0,"msg"=>"更新成功","downCount"=>$downCount];            echo json_encode($arr);        }else{            echo '{"err":1,"msg":"更新失败"}';        }        break;    case "remove":        $id = $_GET["id"];        $lastId = $_GET["lastId"];        $sql = "DELETE FROM weibo WHERE id={$id}";        mysql_query($sql);        if (mysql_affected_rows() > 0){            $sql = "SELECT * FROM weibo WHERE id<{$lastId} ORDER BY id DESC LIMIT 1";            $result = mysql_query($sql);            $row = mysql_fetch_assoc($result);            $arr = array("err"=>0,"msg"=>"删除成功","result"=>$row);            echo json_encode($arr);        }else{            echo '{"err":1,"msg":"删除失败"}';        }        break;    case "page":        $page = $_GET["page"];        $start = $page * 5;        $sql = "SELECT * FROM weibo ORDER BY id DESC LIMIT {$start},5";        $result = mysql_query($sql);        $arr = array("err"=>0,"msg"=>"分页成功","result"=>[]);        while ($row = mysql_fetch_assoc($result)){            array_push($arr["result"],$row);        }            echo json_encode($arr);        break;}

common.php
<?php/** * Created by PhpStorm. * User: dllo * Date: 16/8/16 * Time: 上午9:23 */$mysql = mysql_connect("localhost","root","");date_default_timezone_set("PRC");if (!$mysql){    echo '{"err":1,"msg":"数据库连接失败"}';    // 程序直接退出    exit();}$db = mysql_select_db("0503");if (!$db){    echo '{"err":1,"msg":"0503打开失败"}';    exit();}mysql_query("set names utf8");

0 0
原创粉丝点击