- <form action="test_coreseek.php" method="post">
- <input type="text" value="" name='keyword' /><input type="submit" name='提交' />
- </form>
- <?php
- header('Content-Type:text/html; charset=utf-8');
- $keyword = $_REQUEST['keyword'];
- if($keyword)
- {
- require ( "sphinxapi.php" );
-
- $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
- $pagesize = 10;
- $start = ($page - 1) * $pagesize;
- $cl = new SphinxClient ();
- $cl->SetServer ( '127.0.0.1', 9312);
- $cl->SetConnectTimeout ( 3 );
-
- $cl->SetArrayResult ( true );
- $cl->SetMatchMode ( SPH_MATCH_ANY);
- // 设置分页
- $cl->SetLimits($start, $pagesize, 1000);
- $res = $cl->Query ( $keyword , "*" );
- //dump($cl);
- //dump($res);
- $b = array_reduce($res['matches'], "rsum");
- $mysqli = new mysqli("localhost","root","root","test");
- $ids= join(',',$b); //要把需要的id取出来。
- $sql = "SELECT * FROM documents WHERE id=$id";
- $result = $mysqli->query($sql);
- $data = array();
- $index = "mysql";
- $opts = array
- (
- "before_match" => "<b style='color:red'>",
- "after_match" => "</b>",
- "chunk_separator" => " ... ",
- );
- while($row=$result->fetch_assoc()){
- $res = $cl->BuildExcerpts ( $row, $index, $keyword, $opts );
- echo "<font size=4>".$res[0]."</font></a></br>"; //标题
- echo "<font size=2>".$res[1]."</font></br>"; //摘要
- echo $res[2]."</p><hr>"; //添加时间
- }
- }
- // 将数组中对应的id值添加到新数组
- function rsum($v = array(), $w)
- {
- $v[] = $w['id'];
- return $v;
- }
-
- function dump($var, $echo=true, $label=null, $strict=true) {
- $label = ($label === null) ? '' : rtrim($label) . ' ';
- if (!$strict) {
- if (ini_get('html_errors')) {
- $output = print_r($var, true);
- $output = "<pre>" . $label . htmlspecialchars($output, ENT_QUOTES) . "</pre>";
- } else {
- $output = $label . print_r($var, true);
- }
- } else {
- ob_start();
- var_dump($var);
- $output = ob_get_clean();
- if (!extension_loaded('xdebug')) {
- $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
- $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
- }
- }
- if ($echo) {
- echo($output);
- return null;
- }else
- return $output;
- }
- ?>