網站開發-php開發手機論壇(7)-查看帖子

前面瞭解瞭如何創建帖子和登陸註冊之後,接下來自然是要查看帖子了.

在查看帖子的頁面有點贊和收藏的功能,這裏我一併寫在了一起,其實應該要分開寫比較好.通過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>';
				else
					echo '<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']);

	//每次打開帖子都記錄當前打開的帖子id
	if(isset($_GET['a']))//如果打開新的帖子刷新cookie
		setcookie('cur_aid', $_GET['a'], time()+3600);
		//如果會話沒有被設置,查看是否設置了cookie
	if(!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 "收藏成功";
			else
				echo "收藏失敗,請重試";
			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']);
		//如果會話沒有被設置,查看是否設置了cookie
		if(!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);
	}


?>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章