因近期项目人才网的需求,需要记录搜索关键字及搜索用户uid,因为后面要用到热门关键字和我的最近搜索功能,对于我这个初级菜鸟来说,这方面还是比较欠缺,在对记录搜索用户uid时遇到了一些麻烦,最终加入flag才能解决了这个麻烦。
考虑到这个问题比较典型,特记录以备后来查用,以下为此部分代码:
点击(此处)折叠或打开
- $keys=$_GET['key'];//获取关键字
- $sql = "select id,hits,uid from ".table('searchkey')." where `key` = '".$keys."'";
- $a=$db->getall($sql);
- if($a==""){
- $sqlgo = "INSERT INTO ".table('searchkey')." (`id`,`uid`,`key`,`hits`) VALUES ('','".$_SESSION['uid']."','".$keys."','1')"; //如果没有该关键词记录则插入一条记录
- }else{
- /*如果有记录则搜索次数加1,uid设计为字符串,将uid和目前用户uid逐一对比,存在则不变,不存在则加入目前用户uid*/
- $hi=$a[0]['hits']+1;
- $uidarr=explode(",",$a[0]['uid']);
- $flag=false;
- foreach ($uidarr as $key=>$value){
- if($_SESSION['uid']==$value){
- $flag=true;
- break;
- }
- }
- if(!$flag){
- $a[0]['uid']=$a[0]['uid'].",".$_SESSION['uid'];
- }
- $sqlgo = "update ".table('searchkey')." set `hits`=".$hi.",`uid`='".$a[0]['uid']."' where id = ".$a[0]['id']." and `key`='".$keys."'";
- }
- $db->query($sqlgo);
- //获取简历热搜关键字
- function get_resume_searchkeys($uid,$limi)
- {
- global $db;
- if($uid==0){
- $whilesql = " ";
- $ordersql = " order by hits desc ";
- }else{
- $whilesql = " where uid like '%".$uid."%'";
- $ordersql = " order by time desc ";
- }
- $sql = "select * from ".table('searchkey').$whilesql.$ordersql." limit ".$limi." ";
- return $db->getall($sql);
- }