网站开发-php开发手机论坛(7)-查看帖子

来源:互联网 发布:用友软件教学视频 编辑:程序博客网 时间:2024/05/01 19:37

前面了解了如何创建帖子和登陆注册之后,接下来自然是要查看帖子了.

在查看帖子的页面有点赞和收藏的功能,这里我一并写在了一起,其实应该要分开写比较好.通过ajax不刷新页面进行点赞和收藏.每次访问后台都要进行验证用户身份的操作,主要通过查看session和cookie

templates/view.php

<!DOCTYPE html><html><head><?php header("Content-type: text/html; charset=utf-8");  ?><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.css"><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script src="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.js"></script><script>          $(document).on("pagecreate", function(){              <!-- 利用ajax提交数据,不刷新整个页面 -->        //如果对于ajax不熟悉,请去w3c自学.  $("#fav").click(function()  {  xmlhttp=new XMLHttpRequest();xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {    document.getElementById("fav").innerHTML="点赞"+xmlhttp.responseText;    } } xmlhttp.open("GET", "view.php?fav=1", true); xmlhttp.send();});$("#col").click(function(){    xmlhttp=new XMLHttpRequest();xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {    alert(xmlhttp.responseText);    }  }  xmlhttp.open("GET", "view.php?col=1", true);xmlhttp.send();  });        });      </script>  </head><body><div data-role="page" id="view"><div data-role="header"><a  class="ui-btn-left ui-icon-back ui-btn  ui-btn-inline ui-mini ui-corner-all ui-btn-icon-left "  data-rel="back" >返回</a><h1><?php echo $article['title'] ?></h1><?php if($uid != $article['uid'])echo '<a href="dialog.php?a=',$aid,'&touid=',$article['uid'],'" class="ui-btn-right ui-btn  ui-btn-inline ui-mini ui-corner-all ui-btn-icon-right ui-icon-comment">评论</a>';elseecho '<a href="edit.php?a=',$aid,'" class="ui-btn-right ui-btn  ui-btn-inline ui-mini ui-corner-all ui-btn-icon-right ui-icon-edit">编辑</a>'; ?></div><div role="main" class="ui-content"><?php $edittime=showtime($article['addtime']);if($article['img_path']==null){//设置属性使pre标签自动换行echo '<p><pre style="white-space:pre-wrap; /* css3.0 */white-space:-moz-pre-wrap; /* Firefox */white-space:-pre-wrap; /* Opera 4-6 */white-space:-o-pre-wrap; /* Opera 7 */word-wrap:break-word; /* Internet Explorer 5.5+ */">',$article['content'],'</pre></p><p style="font-size:13px;">发布于',$edittime,'</p>';}else{echo '<p><pre style="white-space:pre-wrap; /* css3.0 */white-space:-moz-pre-wrap; /* Firefox */white-space:-pre-wrap; /* Opera 4-6 */white-space:-o-pre-wrap; /* Opera 7 */word-wrap:break-word; /* Internet Explorer 5.5+ */">',$article['content'],'</pre></p><div style="text-align: center"><img style="width: 90%" src="',$article['img_path'],'"></div><p style="font-size:13px;">发布于',$edittime,'</p>';}?><button data-inline="true" data-mini="true" id="fav" >点赞<?php echo $fav; ?></button><button data-inline="true" data-mini="true" id="col" >收藏</button><ul data-role="listview" data-inset="true" ><li data-role="list-divider">逗论</li><?php while ($comment_=mysql_fetch_array($query)){$touid=$comment_['touid'];$query1=mysql_query("select name from users where id='$touid'");$toname=mysql_fetch_array($query1);Chromephp::log($comment_);$ctime=showtime($comment_['addtime']);echo '<li><img src="',$comment_['cover'],'"><p>',$comment_['name'],' 回复 ',$toname['name'],': ',$comment_['content'],'</p><p>',$ctime,' </p><div><a href="dialog.php?a=',$aid,'&touid=',$comment_['uid'],'" class="ui-btn ui-btn-inline ui-mini ui-icon-comment ui-btn-right ui-btn-icon-right ">回复</a></div></li>';}mysql_free_result($query);?></ul><?php if ($p <= $pagenum-1){# code...// Chromephp::log("评论页数".$pagenum);$p += 1;echo '<a  class="ui-btn  ui-btn-inlineui-corner-all" href="comment.php?a=',$aid,'&p=',$p,'" >更多评论</a>';} ?><form method="POST"  <?php echo 'action="view.php?a=',$_GET['a'],'&touid=',$article['uid'],'"' ?>><div data-role="fieldcontain"><?php echo '<textarea placeholder="回复',$aname['name'],': " name="comment"></textarea>' ?>    <input type="submit" value='提交' id="btn_comment"/></div></form></div></div></body></html>
view.php(接收get参数为:a文章id,col收藏,fav点赞,post接收评论,考虑到评论多的情况,需要分页显示)

<?php include('conn.php');include('lib.php');include('ChromePhp.php');//使用会话内存储的变量值之前必须先开启会话session_start();// Chromephp::log($_SESSION['uid']);// Chromephp::log($_SESSION['name']);//每次打开帖子都记录当前打开的帖子idif(isset($_GET['a']))//如果打开新的帖子刷新cookiesetcookie('cur_aid', $_GET['a'], time()+3600);//如果会话没有被设置,查看是否设置了cookieif(!isset($_SESSION['uid'])){    if(isset($_COOKIE['uid'])&&isset($_COOKIE['name']))    {        //用cookie给session赋值        $_SESSION['uid']=$_COOKIE['uid'];        $_SESSION['name']=$_COOKIE['name'];           }}if($_SERVER['REQUEST_METHOD'] == 'GET'){//处理收藏事件,处理完结束脚本if ($_GET['col']==1) {//判断用户是否登陆if(!isset($_SESSION['uid'])){    if(isset($_COOKIE['uid'])&&isset($_COOKIE['name']))    {        //用cookie给session赋值        $_SESSION['uid']=$_COOKIE['uid'];        $_SESSION['name']=$_COOKIE['name'];           }   else   {   echo "请先登陆";   exit();   }}$aid=$_COOKIE['cur_aid'];$uid=$_SESSION['uid'];//判断用户是否已经收藏过ChromePhp::log($aid);ChromePhp::log($uid);$query=mysql_query("select count(*) as count from collects where aid='$aid' and uid='$uid'");$count=mysql_fetch_array($query);if($count['count']==1){echo '你已经收藏过了';exit();}$time=time();if(mysql_query("insert into collects (uid, aid, time) values('$uid', '$aid', '$time')"))echo "收藏成功";elseecho "收藏失败,请重试";exit();}if($_GET['fav']==1){if(!isset($_SESSION['uid'])){    if(isset($_COOKIE['uid'])&&isset($_COOKIE['name']))    {        //用cookie给session赋值        $_SESSION['uid']=$_COOKIE['uid'];        $_SESSION['name']=$_COOKIE['name'];           }   else   {   exit();   }}$aid=$_COOKIE['cur_aid'];$uid=$_SESSION['uid'];ChromePhp::log($aid);ChromePhp::log($uid);$query=mysql_query("select count(*) as count from favorites where aid='$aid' and uid='$uid'");$count=mysql_fetch_array($query);//如果用户已赞过if($count['count']==1){$query=mysql_query("select count(*) as count from favorites where aid ='$aid'");$result=mysql_fetch_array($query);$fav=$result['count'];echo '('.$fav.')';exit();}//点赞数增加if(mysql_query("insert into favorites (uid, aid) values('$uid', '$aid')")){$query=mysql_query("select count(*) as count from favorites where aid ='$aid'");$result=mysql_fetch_array($query);$fav=$result['count'];echo '('.$fav.')';exit();}}//获取文章作者名字$uid=$_SESSION['uid'];$aid=$_GET['a'];$query=mysql_query("select users.name from articles,users where articles.id='$aid' and articles.uid=users.id");$aname=mysql_fetch_array($query);//获取文章$sql="select * from articles where id = $aid";$query=mysql_query($sql);$article=mysql_fetch_assoc($query);//获取评论//分页代码//计算留言总数$count_result = mysql_query("SELECT count(*) as count FROM comments where aid='$aid'");$count_array = mysql_fetch_array($count_result);//计算总的页数$pagenum=ceil($count_array['count']/$pagesize);//ChromePhp::log($count_array['count']);//确定当前页数 $p 参数$p = $_GET['p']?$_GET['p']:1;//数据指针$offset = ($p-1)*$pagesize;//获取点赞数$query=mysql_query("select count(*) as count from favorites where aid ='$aid'");$result=mysql_fetch_assoc($query);$fav=$result['count'];if($fav==0)$fav="";else$fav='('.$fav.')';// ChromePhp::log($pagenum);// ChromePhp::log($comments);$sql="select * from comments ,users where comments.aid = '$aid' and comments.uid = users.id ORDER BY addtime ASC LIMIT  $offset, $pagesize";$query=mysql_query($sql);include("templates/view.php");}if ($_SERVER['REQUEST_METHOD'] == 'POST'){//使用会话内存储的变量值之前必须先开启会话session_start();// Chromephp::log($_SESSION['uid']);// Chromephp::log($_SESSION['name']);//如果会话没有被设置,查看是否设置了cookieif(!isset($_SESSION['uid'])){    if(isset($_COOKIE['uid'])&&isset($_COOKIE['name']))    {        //用cookie给session赋值        $_SESSION['uid']=$_COOKIE['uid'];        $_SESSION['name']=$_COOKIE['name'];   }   else   {   header("Location: login.php");   exit();   }}$uid=$_SESSION['uid'];//得到被回复者的名字$aid=$_GET['a'];$touid=$_GET['touid'];$query=mysql_query("select name from users where id='$touid'");$toname=mysql_fetch_array($query);//获取文章$aid=$_GET['a'];$sql="select * from articles where id = $aid";$query=mysql_query($sql);$article=mysql_fetch_assoc($query);//数据库插入评论$time=time();$comment=format($_POST['comment']);$sql="insert into comments (uid, touid, aid, addtime, content) values('$uid', '$touid', '$aid', '$time', '$comment')";mysql_query($sql);//更新编辑时间mysql_query("update articles set edittime='$time' where id = '$aid'");// ChromePhp::log($sql);//获取评论//分页代码//计算留言总数$count_result = mysql_query("SELECT count(*) as count FROM comments");$count_array = mysql_fetch_array($count_result);//计算总的页数$pagenum=ceil($count_array['count']/$pagesize);//确定当前页数 $p 参数$p = $_GET['p']?$_GET['p']:1;//数据指针$offset = ($p-1)*$pagesize;$sql="select * from comments ,users where comments.aid = '$aid' and comments.uid = users.id ORDER BY addtime ASC LIMIT  $offset, $pagesize";$query=mysql_query($sql);include("templates/view.php");// ChromePhp::log($pagenum);// ChromePhp::log($comments);}?>




0 0