Web_PHP_DedeCMS_评论管理_点赞功能闲谈;

25 篇文章 0 订阅
24 篇文章 0 订阅

功能:实现评论、点赞功能;

简介:以JQ ajax请求,以PHP服务响应,以JSON数据交互方式来实现评论功能、点赞功能;

->CSS

<span style="font-size:18px;"><!--CSS-->
<style type="text/css">
	#formdiv {
		width:350px; 
		height:125px; 
		position:fixed; 
		top:180px; 
		left:230px; 
		background-color:#CCCCCC; 
		border: 2px #999999 solid; 
		padding: 5px 10px;
		display:none;
		/*透明度*/
		filter:alpha(opacity=90); /*IE滤镜,透明度50%*/
		-moz-opacity:0.9; /*Firefox私有,透明度50%*/
		opacity:0.9;/*其他,透明度50%*/
	}
	#formdiv p{
		margin-top:3px;
		font-weight: bold;
	}
	#formdiv img {
		float: right;
		margin-top: -5px;
		margin-right: -10px;
		cursor:pointer;
	}
	#page_navigation a{
		cursor: pointer;
		margin-right:5px;
	}
</style></span>


->HTML

<span style="font-size:18px;"><!--HTML-->
<div id="formdiv">
	<img src="/images/close.jpg" title="关闭"/>
	<form id="pjform">
		<p>称呼:<input type='text' name='hzname' id='hzname' style='width:150px' value='游客' /></p>
		<p>评价:<textarea name='hzpj' id='hzpj' style='width:98%;height:100'></textarea></p>
		<div align='right' style='height:30px;padding-top:10px; padding-right:2px;'>
			<input type="submit" name="submit" value="提 交" /><input type="reset" name="reset" value="清 除" style="margin-left:10px;" />
		</div>
	</form>
</div>
<div class="main_zh">
	<div>
		<p style="padding-bottom: 10px; padding-left: 18px; border-bottom: 1px dotted; color: rgb(127, 87, 157); font: 30px/36px MicroSoft YaHei;">患者评价</p>
		<dl id="hospital-evaluate-summary">
			<dd class="fl">
				<label>总评:</label>
				<span class="zx-num">★★★★★</span>
			</dd>
			<dd class="fl">
				<label>服务满意度:</label><span class="zx-num">4.9</span>
			</dd>
			<dd class="fl">
				<label>环境满意度:</label><span class="zx-num">4.9</span>
			</dd>
			<dd class="fl">
				<label>疗效满意度:</label><span class="zx-num">4.9</span>
			</dd>
			<dd class="fl">
				<a class="comment" href="javascript:void(0);" id="pinglun">我要评论</a>
			</dd>
		</dl>	
		<br />
		<br />
		<div id="content0">
			<!--评论内容-->		
		</div>
		<div style="clear:both"></div>
	</div>
</div></span>

->JQ

<span style="font-size:18px;"><!--JS-JQ-->
<script type="text/javascript">
	/// 页面加载完成后
	$(document).ready(function() {
		// 显示表单
		$("#pinglun").click(function(){
			$("#formdiv").show("slow", function() {
				$("[name='reset']").click();	// 重置表单;
			});
			return false;
		});
		
		// 关闭表单
		$("#formdiv img").click(function() {
			$("#formdiv").hide("slow");
		});		
		
		// 提交数据
		$("[name='submit']").click(function() {
			// 整理数据
			var hzname = $("#hzname").val();
			var pjdate = (new Date()).toLocaleString();
			var goodclick = Math.round(Math.random() * 1000);
			var pjly = "贴心护理";
			var hzpj = $("#hzpj").val();
			// 验证数据
			if(hzpj.length==0){
				$("#hzpj").css("border","solid 2px #FF0000");
				$("#hzpj").attr("title", "评论内容不能为空!");
				return false;
			}
			
			// 组装数据
			var argStr = "hzname="+hzname+"&pjdate="+pjdate+"&goodclick="+goodclick+"&pjly="+pjly+"&hzpj="+hzpj+"&action=add";
						
			// 提交数据
			$.ajax({   
				type: "POST",   
				url: "/commentMng.php", 
				data: encodeURI(encodeURI(argStr)),   // 两次编码中文;
				cache:false, //不缓存此页面; 
				success:function(cbdata){  					
					var cbObj = JSON.parse(cbdata);		// JSON解析器;
					if(cbObj.success) {
						alert("评价已提交,静等审核!谢谢您对我们的支持;");
						$("#formdiv").hide("slow");
					}else{
						alert("正在维护,请过后再评论!谢谢您对我们的支持;");
					}
				}   
			});  
			return false;
		});
		
		// 请求评论信息
		$.ajax({
			type: "POST",   
			url: "/commentMng.php", 
			data: "action=get",
			cache: false,
			success: function(data){
				// 处理json数据_{"total": 4, records: [{"name":"value"}, {"name":"value"}]};
				var jsObj = eval("(" + data + ")");		// 记得把json字符串括号起来,以避免语法错误;
				// 评论显示;
				plnrShow(jsObj);
			}
		});
		
	});
	
	///评论内容显示
	function plnrShow(jsObj) {
		var records = jsObj.total;
		var rArray = jsObj.records;
		var pages = Math.floor(records/3) + (records%3==0?0:1);
		var htmlstr = "";
		var pagenum = 1;
		for(var i=0; i<records; i++) {
			// 头
			if(i%3==0) {
				htmlstr = htmlstr + '<div id="contentPage' + pagenum + '" style=" display:' + (i==0?"block":"none") + ';">';
				pagenum += 1;
			}
			
			// 循环
			htmlstr += '<div style="display: block;" class="box07_bglf">';
			htmlstr += '	<p>';
			htmlstr += '		用户:<b>' + rArray[i]["hzname"] + '</b>      评分:';
			htmlstr += '		<img src="/templets/default/images/wujx.png" height="20" width="114">';
			htmlstr += '		<span style="float:right">';
			htmlstr += '			<a title="点赞" οnclick="zanFn(' + rArray[i]["id"] + ');">';
			htmlstr += '				<img id="zanImg' + rArray[i]["id"] + '" style="cursor:pointer" src="/templets/default/images/dmz.jpg">';
			htmlstr += '				(<span style="margin-top:3px;" id="ajax_' +rArray[i]["id"] + '">' + rArray[i]["goodclick"] + '</span>)';
			htmlstr += '			</a>';
			htmlstr += '		</span>';
			htmlstr += '	</p>      ';
			htmlstr += '	<span style="width:680px;">' + rArray[i]["hzpj"] + '</span>';
			htmlstr += '</div>';
			
			// 尾
			if((i+1)%3==0 || i==(records-1)) {	// 右边解决最后一页不满页的情况;
				htmlstr = htmlstr + '</div>';
			}
		}
		
		// 分页
		htmlstr += '<div id="page_navigation">';
		htmlstr += '	<a id="prePage" οnclick="pageTurn(-1,'+ pages +')" style=" display: none;">上一页</a> ';
		for(var p=1; p<=pages; p++) {
			htmlstr += '<a id="pagenum' + p + '" style="color:' + (p==1?"red":"") + ';font-weight:700;" οnclick="pageShow(' + p + ', ' + pages + ')">' + p + '</a>';
		}		
		
		if(pages>1) {
			htmlstr += '<a id="nextPage" οnclick="pageTurn(1,'+ pages +')">下一页</a>';
		}		
		htmlstr += '</div>';
		
		// 输出
		$("#content0").html(htmlstr);		
	}
	
	///分页功能
	var lastPage = 1;	// 默认第一页显示;
	// 元素切换
	function elTurn(nowPage, pages){
		// 原来状态
		$("#pagenum"+lastPage).css("color", "");
		$("#contentPage"+lastPage).css("display","none");
		lastPage = nowPage;
		
		// 当前状态
		$("#pagenum"+nowPage).css("color", "red");
		$("#contentPage"+nowPage).css("display","block")
		
		// 下一页
		if(nowPage == pages) {
			$("#nextPage").css("display", "none");
		}else {
			$("#nextPage").css("display", "");
		}
		
		// 上一页
		if(nowPage>1) {
			$("#prePage").css("display","");
		}else {
			$("#prePage").css("display","none");
		}
	}
	// 页数翻转
	function pageShow(nowPage, pages){
		elTurn(nowPage, pages);
	}
	// 上一下一页
	function pageTurn(increment, pages) {
		var nownum = lastPage + increment;
		if(nownum<= pages && nownum>0) {			
			elTurn(nownum, pages);
		}
	}
	
	<!--点赞功能-->
	function zanFn(id){
		$("#zanImg"+id).fadeOut(300);
		// 请求
		$.ajax({ 
			type:"POST", 
			url:"/commentMng.php", 
			data:"id=" + id + "&action=zan",
			cache:false, //不缓存此页面;
			success:function(data){
				$("#ajax_"+id).html(data);
				$("#zanImg"+id).fadeIn(300);
			} 
		});
	}
</script>
</span>

->PHP

<span style="font-size:14px;"><span style="font-size:18px;"><?php
	// 功能支持
	require_once(dirname(__FILE__)."/include/common.inc.php");  
	// 动作参数
	$action = $_POST['action']; 
	if(!isset($action) || empty($action)){
		exit;
	}
	
	/// 添加评论
	if($action == "add"){
		// 读取参数
		$hzname = iconv("utf-8","gbk",urldecode($_POST["hzname"])); 
		$pjdate = iconv("utf-8","gbk",urldecode($_POST["pjdate"]));
		$goodclick = iconv("utf-8","gbk",urldecode($_POST["goodclick"]));
		$pjly = iconv("utf-8","gbk",urldecode($_POST["pjly"]));
		$hzpj = iconv("utf-8","gbk",urldecode($_POST["hzpj"]));
	
		// 插入数据
		$msg = $db->ExecuteNoneQuery("INSERT INTO dede_diyformhzpj(hzname, hzpj, goodclick, pjly, pjdate) VALUES('$hzname','$hzpj','$goodclick','$pjly','$pjdate');");
		
		// 结果响应
		if($msg == 1) {
			echo '{"success": true, "msg": "添加成功"}';	
		}else {
			echo '{"success": false, "msg": "添加错误"}';
		}
	
	/// 点赞功能
	}else if($action=="zan") {
		$id = $_POST["id"];	// 评论ID;
		
		$plsql = "SELECT * FROM dede_diyformhzpj WHERE id=".$id;
		$db->SetQuery($plsql);
		$db->Execute();
		$count = $db->GetTotalRow();
		if($count==1) {
			// 点赞
			$db->ExecuteNoneQuery("UPDATE dede_diyformhzpj SET goodclick = goodclick + 1 WHERE id = ".$id);
			// 赞数
			$row = $db->GetOne("SELECT * FROM dede_diyformhzpj WHERE id= ".$id);
			$zanTimes = $row['goodclick'];
			echo $zanTimes;
		}else{
			echo "Error!";
		}
		
	/// 获取评论信息
	}else if($action == "get") {
		// 获取数据
		$db->SetQuery("SELECT id,hzname,hzpj,goodclick,pjly,pjdate FROM dede_diyformhzpj WHERE ifcheck = 0 ORDER BY id DESC");   
		$db->Execute();   
		$count = $db->GetTotalRow();
		
		$jsonstr = '{"total": '.$count;
		// 整理数据
		// 拼装json格式 {"total": 4, records: [{"name":"value"}, {}]}
		$index = 0;
		while($row = $db->GetArray()) {
			$index = $index + 1;
			if($index == 1) {
				$jsonstr = $jsonstr.', records: [';
			}
			
			$jsonstr = $jsonstr.'{"id":'.$row["id"].', "hzname": "'.$row["hzname"].'", "hzpj": "'.$row["hzpj"].'", "goodclick": "'.$row["goodclick"].'"},';			
			
			if($index == $count) {
				$jsonstr = substr($jsonstr,0,strlen($jsonstr)-1);	// 删除最后的逗号; 
				$jsonstr = $jsonstr.']';
			}
		}
		$jsonstr = $jsonstr.'}';
		
		// 响应数据
		echo $jsonstr;
	}	

?></span></span>


特别说明:如果能帮助到您,请您留下点滴痕迹,让我知道我的存在是有意义的;如果不能帮助到您,请接受我的歉意;


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值