這篇文章主要介紹了關於PHP後台評論的實現,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
/** * 添加評論 * content 評論內容 * addtime 評論時間 * uid 評論作品 */ public function padd(){ $param=input('param.'); $param['pid']=session('tid'); if(empty($param['uid'])){ return json(['code'=>2,'message'=>'uid不可為空']); }$data=db('comments')->insertGetId(['uid'=>$param['uid'],'content'=>$param['content'],'addtime'=>$param['addtime'],'pid'=>$param['pid']]); if($data){ return json(['code'=>1,'message'=>'評論成功']); }else{ return json(['code'=>2,'message'=>'評論失敗']); } }/** * 評論展示 * uid comments表 * page 頁數 * num 條數 */ public function comment(){ $param=input('param.'); $param['pid']=session('tid');//使用者id if(empty($param['page'])){ $param['page']=1; }if(empty($param['num'])){ $param['num']=5; } if(empty($param['uid'])){ return json(['code'=>2,'message'=>'uid不可為空']); }$data=db('comments')->where('uid',$param['uid'])->page($param['page'],$param['num'])->select();$commentslike=db('commentslike'); $people=db('people'); foreach ($data as $k=>$value) { //查看評論點贊表的狀態 $state=$commentslike->where(['pid'=>$param['pid'],'uid'=>$param['uid'],'cid'=>$value['data']])->value('state'); if(empty($state)){ $state = '還未點贊'; } $data[$k]['state']=$state;//擷取評論人的手機號 $people=$people->where('id',$value['pid'])->value('tel'); $data[$k]['tel']=$people; //把二級評論添加到數組資料中 if($value['rpid'] !== 0){ // $r_pid = $commentslike->where(['id'=>$value['rpid']])->value('pid'); $r_tel = $people->where('id',$value['rpid'])->value('tel'); $data[$k]['r_tel'] = $r_tel; }else{ $data[$k]['r_tel'] = ''; }} return json(['code'=>1,'data'=>$data]); }/** * 評論點贊 * uid 作品id */ public function commentslike(){ $param=input('param.'); $param['pid']=session::get('tid');if(empty($param['uid'])){ return json(['code'=>2,'message'=>'uid不可為空']); } //判斷該作品有沒有評論 $comments=db('comments')->where('uid',$param['uid'])->select(); if(!empty($comments)){ //判斷他有沒有點贊 $data=db('commentslike')->where(['uid'=>$param['uid'],'pid'=>$param['pid'],'cid'=>$comments['id']])->find(); if(empty($data)){ $like=db('commentslike')->insertGetId(['uid'=>$param['uid'],'pid'=>$param['pid'],'cid'=>$comments['id'],$param['state']=>'點贊']); if($like){ return json(['code'=>1,'message'=>'點贊成功']); }else{ return json(['code'=>1,'message'=>'點贊失敗']); } }else{ if($data['state']=='取消點贊'){ $like=db('commentslike')->where(['uid'=>$param['uid'],'pid'=>$param['pid'],'cid'=>$comments['id']])->update([$param['state']=>'點贊']); }elseif($data['state']=='點贊'){ $like=db('commentslike')->where(['uid'=>$param['uid'],'pid'=>$param['pid'],'cid'=>$comments['id']])->update([$param['state']=>'取消點贊']); } }}else{ return json(['code'=>2,'message'=>'此作品還未評論']); } }/** * 回複評論 * uid 作品id * content 回複內容 * cid 這條評論的id * uid 作品id pid 評論人id */ public function reply(){ $param['pid'] = session('tid'); $param = input('param.'); //被評論的id $param['cid'] $data = db('comments')->where(['id'=>$param['rid']])->find(); if($data['rid'] !== 0){ $param['rid'] = $data['rid'];//被回複人的pid. } $param['rid'] = $data['id'];//被回複人的pid.$param['rpid'] = $data['pid'];//評論內容 $param['content'] //作品id $param['uid'] $param['addtime'] = date('Y-m-d H:i:s'); $id = db('comments')->insertGetId(['uid'=>$param['uid'],'content'=>$param['content'],'addtime'=>$param['addtime'],'pid'=>$param['pid'],'rid'=>$param['rid']]); if($id){ return json(['code'=>1,'messgae'=>'回複成功']); }else{ return json(['code'=>2,'message'=>'回複失敗']); } } /** * 查看二級評論 * uid 作品id * rid 這條評論的id */ public function GetRCommtens($value='') { $data = db('comments')->where(['rid'=>$param['rid']])->select(); }
以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!