<?php

#====================
# For more examples see: http://www.nearby.org.uk/sphinx/search-example.php

# Change this settings to match your setup... 

$CONF = array();

$CONF['sphinx_host'] = 'localhost';
$CONF['sphinx_port'] = 1238;

$CONF['mysql_host'] = "localhost";
$CONF['mysql_username'] = "user";
$CONF['mysql_password'] = "password";
$CONF['mysql_database'] = "data";

$CONF['sphinx_index'] = "myindex";

#mysql query to fetch results needs id, title and body columns
#<ids> is replaced by the list of ids
#this query can be as arbitary complex as required - but mysql has be able to run it quickly
#don't include a order by

$CONF['mysql_query'] = '
SELECT page_id AS id,title AS title,description AS body
FROM table
WHERE page_id IN ($ids)
'
;

#can use 'excerpt' to highlight using the query, or 'asis' to show description as is.
$CONF['description'] = 'asis'

#the linke for the title
$CONF['link_format'] = '/page.php?page_id=$id';

//might need to put in path to your file
require ( "sphinxapi.php" );

#====================

#Nothing below should need changing

$q = isset($_GET['q'])?$_GET['q']:'';

$q preg_replace('/ OR /',' | ',$q);

$q preg_replace('/[^\w~\|\(\)\^\$\?-]+/',' ',trim(strtolower($q)));

?>
        <form action="?" method="get">
        Search: 
                <input name="q" type="text" value="<? echo htmlentities($q); ?>"/>
                <input type="submit" value="Search"/>
        </form>
<?

if (!empty($q)) {
    
//not needed as we strip all funny chars anyway!
    #$q = mysql_real_escape_string($q);

    
$qo $q;
    if (
strlen($qo) > 64) {
        
$qo '--complex query--';
    } 
    
    if (
1) {
        
//text query
        // --------------
        
        
$mode SPH_MATCH_ALL;
        if (
strpos($q,'~') === 0) {
            
$q preg_replace('/^\~/','',$q);
            if (
substr_count($q,' ') > 1//over 2 words
                
$mode SPH_MATCH_ANY;
        } elseif (
strpos($q,'-') !== FALSE || strpos($q,'|') !== FALSE) {
            
$mode SPH_MATCH_EXTENDED;
        } 
        
$cl = new SphinxClient ();
        
$cl->SetServer $CONF['sphinx_host'], $CONF['sphinx_port'] );
        
$cl->SetSortMode SPH_SORT_EXTENDED"@relevance DESC, @id DESC" );
        
$cl->SetMatchMode $mode );
        
$cl->SetLimits(0,25);
        
$res $cl->Query $q$CONF['sphinx_index'] );
        
        
// --------------
        
        
if ( $res===false )
        {
            print 
"\tQuery failed: -- please try again later.\n";
            exit;
        } else
        {
            if ( 
$cl->GetLastWarning() )
                print 
"\nWARNING: " $cl->GetLastWarning() . "\n\n";
        
            
$query_info "Query '$qo' retrieved ".count($res['matches'])." of $res[total_found] matches in $res[time] sec.\n";
            
        
        }
        
        if (
is_array($res["matches"]) ) {
        
            
$ids array_keys($res["matches"]);
            
            
$sql str_replace('$ids',$ids,$CONF['mysql_query']);    
        } else {
            
$r "\t--none--";
        }
            
    }
    
    if (
$sql) {
        
$db mysql_connect($CONF['mysql_host'],$CONF['mysql_username'],$CONF['mysql_password']);
        
mysql_select_db($CONF['mysql_database'], $db);
        
        
$result mysql_query($sql) or die ("Couldn't select query : $sql " mysql_error() . "\n");
        
$r '';
        if (
mysql_num_rows($result) > 0) {
            print 
"<ol id=\"results\">";        
            
$rows = array();
            while (
$row mysql_fetch_array($result,MYSQL_ASSOC)) {
                
$rows[$row['gridimage_id']] = $row;
            }
            foreach (
$ids as $c => $id) {
                
$row $rows[$id];
                
                
$link htmlentities(str_replace('$id',$row['id'],$CONF['link_format']));

                print 
"<li><a href=\"$link\">".htmlentities($row['title'])."</a><br/>";

                print 
htmlentities($row['body'])."</li>";

            }
            print 
"</ol><pre>$query_info</pre>";
        } else {
            print 
"--none--";
        }
    } 
    
}