sphinx 搜索分页 php CI实例

sphinx 搜索分页 php CI实例,model里面读取数据的方法:

function getSearchResult($keyword = ”,$page = 1,$page_size = 10) {
$query = array ();
try {
$cl = new SphinxClient ();
$cl->SetServer ( ‘localhost’, 9321 ); // 注意这里的端口号
$cl->SetMatchMode ( SPH_MATCH_EXTENDED ); // 使用多字段模式
$cl->SetLimits(($page-1)*$page_size,$page_size);//传递当前页面所需的数据条数的参数
$index = “business_info”;//索引名称
$query = $cl->Query ( $keyword, $index );
$cl->close ();
} catch ( Exception $e ) {
}
return $query;
}

分页效果图:

分页效果图

//调用代码

$data['name'] = $name;
$data['page'] = $page;
$page_size = 10;

$result = $this->searchmodel->getSearchResult($name,$page,$page_size);
// var_dump($result);
$data['rscount'] = $result['total'];
$data['searchResult'] = $result['matches'];

$totalpages = intval($data['rscount']/$page_size);
$adjacents = 3;
$reload = base_url().’search/’.$name;
$data['pages'] = pages($reload, $page, $totalpages, $adjacents,$data['rscount']);

//分页 pages的方法

function pages($reload, $page, $totalpages, $adjacents,$total,$split=”/”) {

$firstlabel = “« ”;
$prevlabel = “‹ Previous”;
$nextlabel = “Next ›”;
$lastlabel = “ »”;

$out = “<div class=\”pagin\”>\n”;
$out .= “<span class=\”totals\”>Results:”.($total).”</span>”;
// first
if($page>($adjacents+1)) {
$out.= “<a rel=\”nofollow\” href=\”” . $reload . “\”>” . $firstlabel . “</a>\n”;
}
else {
$out.= “<span>” . $firstlabel . “</span>\n”;
}

// previous
if($page==1) {
$out.= “<span>” . $prevlabel . “</span>\n”;
}
elseif($page==2) {
$out.= “<a rel=\”nofollow\” href=\”” . $reload . “\”>” . $prevlabel . “</a>\n”;
}
else {
$out.= “<a rel=\”nofollow\” href=\”” . $reload . $split . ($page-1) . “\”>” . $prevlabel . “</a>\n”;
}

// 1 2 3 4 etc
$pmin = ($page>$adjacents) ? ($page-$adjacents) : 1;
$pmax = ($page<($totalpages-$adjacents)) ? ($page+$adjacents) : $totalpages;
for($i=$pmin; $i<=$pmax; $i++) {
if($i==$page) {
$out.= “<span class=\”current\”>” . $i . “</span>\n”;
}
elseif($i==1) {
$out.= “<a rel=\”nofollow\” href=\”” . $reload . “\”>” . $i . “</a>\n”;
}
else {
$out.= “<a rel=\”nofollow\” href=\”” . $reload . $split . $i . “\”>” . $i . “</a>\n”;
}
}

// next
if($page<$totalpages) {
$out.= “<a rel=\”nofollow\” href=\”” . $reload . $split .($page+1) . “\”>” . $nextlabel . “</a>\n”;
}
else {
$out.= “<span>” . $nextlabel . “</span>\n”;
}

// last
if($page<($totalpages-$adjacents)) {
$out.= “<a rel=\”nofollow\” href=\”” . $reload . $split . $totalpages . “\”>” . $lastlabel . “</a>\n”;
}
else {
$out.= “<span>” . $lastlabel . “</span>\n”;
}

$out.= “</div>”;

return $out;
}

 

暂无评论

发表评论

快捷键:Ctrl+Enter