🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 检索出除自己外呢称含有关键字的用户 ``` $where = array( 'username' => array('LIKE', '%' . $keyword . '%'), 'uid' => array('NEQ', session('uid')) ); ``` ## 互相关注组合条件 ``` $db = M('follow'); $sql = '(SELECT `follow` FROM `hd_follow` WHERE `follow` = ' . $v['uid'] . ' AND `fans` = ' . session('uid') . ') UNION (SELECT `follow` FROM `hd_follow` WHERE `follow` = ' . session('uid') . ' AND `fans` = ' . $v['uid'] . ')'; $mutual = $db->query($sql); ``` >完整代码: ``` /** * 重组结果集得到是否互相关注与是否已关注 * @param [Array] $result [需要处理的结果集] * @return [Array] [处理完成后的结果集] */ Private function _getMutual ($result) { if (!$result) return false; $db = M('follow'); foreach ($result as $k => $v) { //是否互相关注 $sql = '(SELECT `follow` FROM `hd_follow` WHERE `follow` = ' . $v['uid'] . ' AND `fans` = ' . session('uid') . ') UNION (SELECT `follow` FROM `hd_follow` WHERE `follow` = ' . session('uid') . ' AND `fans` = ' . $v['uid'] . ')'; $mutual = $db->query($sql); if (count($mutual) == 2) { $result[$k]['mutual'] = 1; $result[$k]['followed'] = 1; } else { $result[$k]['mutual'] = 0; //未互相关注是检索是否已关注 $where = array( 'follow' => $v['uid'], 'fans' => session('uid') ); $result[$k]['followed'] = $db->where($where)->count(); } } return $result; } ``` ## 格式化搜索关键字(加红搜索关键字) ``` {$v.username|str_replace=$keyword, '<font style="color:red">' . $keyword . '</font>', ###} <a href="">{$v.username|str_replace=$keyword, '<font style="color:red">' . $keyword . '</font>', ###}</a> ``` ## 搜索找人后台处理 ``` /** * 搜索找人 */ Public function sechUser () { $keyword = $this->_getKeyword(); if ($keyword) { //检索出除自己外呢称含有关键字的用户 $where = array( 'username' => array('LIKE', '%' . $keyword . '%'), 'uid' => array('NEQ', session('uid')) ); $field = array('username', 'sex', 'location', 'intro', 'face80', 'follow', 'fans', 'weibo', 'uid'); $db = M('userinfo'); //导入分页类 import('ORG.Util.Page'); $count = $db->where($where)->count('id'); $page = new Page($count, 20); $limit = $page->firstRow . ',' . $page->listRows; $result = $db->where($where)->field($field)->limit($limit)->select(); //重新组合结果集,得到是否已关注与是否互相关注 $result = $this->_getMutual($result); //分置搜索结果到视图 $this->result = $result ? $result : false; //页码 $this->page = $page->show(); } $this->keyword = $keyword; $this->display(); } ``` ### 返回搜索关键字 ``` Private function _getKeyword () { return $_GET['keyword'] == '搜索微博、找人' ? NULL : $this->_get('keyword'); } ``` ### 检查是否互相关注 ``` /** * 重组结果集得到是否互相关注与是否已关注 * @param [Array] $result [需要处理的结果集] * @return [Array] [处理完成后的结果集] */ Private function _getMutual ($result) { if (!$result) return false; $db = M('follow'); foreach ($result as $k => $v) { //是否互相关注 $sql = '(SELECT `follow` FROM `hd_follow` WHERE `follow` = ' . $v['uid'] . ' AND `fans` = ' . session('uid') . ') UNION (SELECT `follow` FROM `hd_follow` WHERE `follow` = ' . session('uid') . ' AND `fans` = ' . $v['uid'] . ')'; $mutual = $db->query($sql); if (count($mutual) == 2) { $result[$k]['mutual'] = 1; $result[$k]['followed'] = 1; } else { $result[$k]['mutual'] = 0; //未互相关注是检索是否已关注 $where = array( 'follow' => $v['uid'], 'fans' => session('uid') ); $result[$k]['followed'] = $db->where($where)->count(); } } return $result; } ``` ## 模板显示搜索数据 ``` <if condition="isset($result)"> <div id='content'> <if condition='$result'> <div class='view_line'> <strong>用户</strong> </div> <ul> <foreach name='result' item='v'> <li> <dl class='list-left'> <dt> <img src=" <if condition='$v["face80"]'> __ROOT__/Uploads/Face/{$v.face80} <else/> __PUBLIC__/Images/noface.gif </if>" width='80' height='80'/> </dt> <dd> <a href="">{$v.username|str_replace=$keyword, '<font style="color:red">' . $keyword . '</font>', ###}</a> </dd> <dd> <i class='icon icon-boy'></i>&nbsp; <span> <if condition='$v["location"]'> {$v.location} <else/> 该用户未填写所在地 </if> </span> </dd> <dd> <span>关注 <a href="">{$v.follow}</a></span> <span class='bd-l'>粉丝 <a href="">{$v.fans}</a></span> <span class='bd-l'>微博 <a href="">{$v.weibo}</a></span> </dd> </dl> <dl class='list-right'> <if condition='$v["mutual"]'> <dt>互相关注</dt> <dd class='del-follow' uid='{$v.uid}' type='1'>移除</dd> <elseif condition='$v["followed"]' /> <dt>√&nbsp;已关注</dt> <dd class='del-follow' uid='{$v.uid}' type='1'>移除</dd> <else/> <dt class='add-fl' uid='{$v.uid}'>+&nbsp;关注</dt> </if> </dl> </li> </foreach> </ul> <div style="text-align:center;padding:20px;">{$page}</div> <else/> <p style='text-indent:7em;'>未找到与<strong style='color:red'>{$keyword}</strong>相关的用户</p> </if> </div> </if> ```