1.创建方法时,不要用index这类的不易显示该方法功能的单词,尽量使用功能的缩写
例如:
public function xssc(){}
2.尽量少用Request方法,多用input助手方法获取请求值
例如
//获取请求值 $data = input(); //把所有请求都放到$data里 $token = $data['token']; //token值 $nsid = $data['nsid']; //新闻/视频id $uid = $data['uid']; //用户id $type = $data['type']; //类型 1为新闻,2为视频 dump($data);
3.数据库操作
(1)原生操作:query()
$cms = Db::query("select users.headimg,users.nickname,newreview.ncommtent as commentcontent,newreview.ncotime as cotime,news.thumbnum from users,newreview,news where
newreview.uid = $uid and users.uid = newreview.uid and newreview.nid = news.nid ");
(2)name查询 (注意,要修改配置的数据库前缀为 ' ')
$list = Db::name('newcollect')->insert(['nid' => $nsid, 'uid' => $uid]);
4.返回JSON数组
//返回值 $res = array( 'code'=>$code, 'msg'=>array( 'newvideos'=>$newvideos, ) );
echo json_encode($res,256); //256为哪怕是汉字也可以输出,不然格式不对
5.往数组里添加数组
$cms['isthumb']=$isthumb;
这样显示的结果在数组外面
foreach($cms as $v){ //查询该条新闻用户是否点赞过 $spl = Db::query("select count(*) from nthumbid where uid = $uid and nid = $xsid and nrid = $comments"); if($spl > 0){ $isthumb = 0; //表示用户点赞过 } else { $isthumb = 1; //表示用户没有点赞过 } $v['isthumb']=$isthumb; }
这样就添进去了
还没有人抢沙发呢~