自动补全就是自动提示,类似于搜索引擎,再上面输入一个字符,下面会提示多个关键词供参考,比如你输入 nb 2字符, 会自动提示nba,nba录像,nba直播 。
原理:
1, 将所有的游戏名字读出来,拆分成单个汉字
2, 将这些汉字作为redis集合的键,写入redis,每个集合里的值是所有那些游戏名字中包含此汉字的游戏的id
3, 当用户输入文字的时候通过ajax异步请求,将用户输入传给PHP
4, 将输入的文字拆分成单个汉字, 分别找到这些汉字在redis中的集合值
5, 取出来,求交集,就找到了同时包含这几个汉字的游戏的id
6, 最后到数据库里查出来相应的游戏信息即可
缺点: 删除数据不方便
//自动补全, 建立索引
public function setAutoComplateAction()
{
set_time_limit(0);
$arrGame =M('game')->field('name,id')->where('type_id != 5')->select();
$arrGameIdAndName=array();
foreach ( $arrGame as $item ) {
$arrGameIdAndName[ $item['id'] ] = $item['name'] ;
}
$arrIndex = array();
foreach ($arrGameIdAndName as $gid => $gname) {
$intGnameLength = mb_strlen($gname, 'UTF-8');
for ($i=0; $i < $intGnameLength; $i++) {
$strOne = mb_substr($gname, $i, 1, 'UTF-8');
$arrIndex[$strOne][] = $gid;
}
}
foreach ($arrIndex as $word => $arrGid) {
foreach ($arrGid as $gid) {
$this-> sadd( 'game_'.$word, $gid);
}
}
$this->success();
}
public function sadd($key, $value)
{
$redis = new Predis(15);
$client = $redis->getClient();
$number = $client->sadd($key, $value);
}
//自动补全
//不限输入汉字的前后顺序: 输入"国三杀" => 输出 "三国杀"
public function getAutoComplate()
{
$word = I('get.keyword', '');
// $word = '王者';
if (empty($word)) {
$arrGame= M('game')->field('id,name')->limit(20)->select();
$this->ajaxReturn( $arrGame );
}
$intWordLength = mb_strlen($word, 'UTF-8');
if (1 == $intWordLength) {
$arrGid = $this->getAuto($word);
} else {
$arrGid = array();
for ($i=0; $i < $intWordLength; $i++) {
$strOne = mb_substr($word, $i, 1, 'UTF-8');
$arrGidTmp = $this->getAuto($strOne);
$arrGid = empty($arrGid) ? $arrGidTmp : array_intersect($arrGid, $arrGidTmp); //求交集,因为传入的参数个数不确定,因此不能直接求交集
}
}
$map=array();
if($arrGid){
$map['id'] = array('in',$arrGid);
$arrGame =M('game')->field('name,id')->where( $map)->select();
}
// var_dump($arrGame);exit;
// $jsonGame = json_encode($arrGame);
$this->ajaxReturn( $arrGame );
}
//自动补全功能
public function getAuto($key)
{
$youxikey = 'game_'.$key;
$redis = new Predis(15);
$client = $redis->getClient();
return $client->smembers($youxikey);
}
数据库中game表中的测试数据入下截图所示