Windows to the Universe Search
'Principiante', 'mid' => 'Intermedio', 'high' => 'Avanzado' ); } else { $text_results = "Search Results for "; $text_return = "Back to search options "; $text_level = "Note: Following the links below will change your level from "; $text_to = " to "; $text_check = "Please check at least one search area. "; $text_tryagain = "Try again."; $text_nomatch = "Sorry, no matches were found."; $text_found = "Found "; $text_matches = " matches"; $text_toomany = "Too many results, try to narrow your search."; $text_page = "Page "; $text_next = "Next"; $level_name = array ( 'elem'=> 'Beginner', 'mid' => 'Intermediate', 'high' => 'Advanced' ); } $RESULTS_PER_PAGE = 20; $search_phrase = trim($_GET["search_phrase"]); $search_phrase = htmlentities( $search_phrase ); // remove html $Words = explode( ' ', $search_phrase ); $list = array(); $query_string = ''; if ( $search_level != 'mid' ) $query_string .= "&edu=$search_level"; if ( $lang != 'en' ) $query_string .= "&lang=$lang"; $db = connect(); if ( $search_level != $level ) { $Warning = "$text_level $level_name[$level] $text_to $level_name[$search_level]."; } $select = ''; if ( $_GET["search_dir"] ) { $select = " AND Dir Like '/$_GET[search_dir]%' "; } $query = "SELECT PageID, Value, Dir, Page, MATCH (value) AGAINST ('$search_phrase') AS score FROM win_content, win_pages WHERE " . "win_content.PageID=win_pages.ID $select AND (Level='$search_level' OR (Level='high' and Template like '%nolevels')) AND Lang='$lang' AND Element=2 AND MATCH (value) AGAINST ('$search_phrase') LIMIT 1000"; //print ""; $result=mysql_query($query, $db) ; if ( !$result ) { show_error("Error finding content" . mysql_error()); exit; } if ( mysql_num_rows($result) < 1 ) { // $msg = "Sorry, no results"; $num_rows = 0; } else { $num_rows = mysql_num_rows( $result ); while( $row = mysql_fetch_assoc($result) ) { // get rid of comments and html tags. If no match after this, skip. $text = preg_replace( "//s", " ", $row["Value"] ); $text = preg_replace( "/<[^<>]+>/s", " ", $text ); $match = 0; foreach ( $Words as $word ) { if ( preg_match( "/$word/i", $text ) ) $match = 1; } if ( ! $match ) { $num_rows--; continue; } $title = get_content_element_byids( 25, $row["PageID"], $level, $lang ) ; $score = $row["score"]; foreach ( $Words as $word ) { if ( strtolower(trim($title)) == strtolower(trim($word) )) { $score *= 1000; } elseif ( preg_match( "/^$word/i", $title ) ) { $score *= 100; } elseif ( preg_match( "/$word/i", $title ) ) { $score *= 20; } } $list[] = array($row["PageID"], $row["Dir"].$row["Page"], $text, $title, $score); // print $row["PageID"]; } usort( $list, "byscore" ); for ( $line=0; $line<$num_rows; $line++ ) { if ( $line < ( $currPage - 1 ) * $RESULTS_PER_PAGE ) { continue; } if ( $line >= $currPage * $RESULTS_PER_PAGE ) { break; } $result = $list[$line]; // $title = get_content_element_byids( 1, $result[0], $level, $lang ) ; $title = $result[3]; /////////////// Choose first 3 sentences that have search words // $linkBlurb = "score " . $result[4]; $linkBlurb = ''; // get rid of html tags. If no match after this, skip. // $text = preg_replace( "/<[^<>]+>/", " ", $result[2] ); $Sentences = preg_split ('/[?!.]\s+/', $result[2]); $numSentences = 0; foreach ( $Sentences as $s ) { $hasWords = 0; $found = 0; foreach ( $Words as $word ) { $s = preg_replace( "/([^\w><])($word\w*)([^\w><])/i", "$1$2$3", $s, -1, $count ); if ( $count > 0 ) { $found = 1; } $s = preg_replace( "/^($word\w*)([^\w><])/i", "$1$2", $s, -1, $count ); if ( $count > 0 ) { $found = 1; } $s = preg_replace( "/([^\w><])($word\w*)$/i", "$1$2", $s, -1, $count ); if ( $count > 0 ) { $found = 1; } $title = preg_replace( "/([^\w><])($word\w*)([^\w><])/i", "$1$2$3", $title ); $title = preg_replace( "/^($word\w*)([^\w><])/i", "$1$2", $title ); $title = preg_replace( "/([^\w><])($word\w*)$/i", "$1$2", $title ); } if ( $found ) { $linkBlurb .= "$s..."; $numSentences++; if ($numSentences > 2) break; } } /////////// end choosing sentences $display .= "
$linkBlurb
\n"; } } $Pages = intval( ($num_rows - 1) / $RESULTS_PER_PAGE ) + 1; if ( !$num_rows ) { if ( $typesEmpty ) { $FMBlurb = "$text_check $text_tryagain"; } else { $FMBlurb = "Sorry, no matches were found. $text_tryagain"; } } else { // if ( $num_rows > 1 ) { $Plur = "es"; } $FMBlurb = "${Warning}$text_found $num_rows $text_matches:"; if ( $num_rows == 1000 ) { $FMBlurb .= "
$text_toomany"; } $query_string = ''; foreach ( $_GET as $key=>$value ) { if ( $key != 'page' ) $query_string .= "&$key=" . urlencode($value); } $pageLinks = ""; if ( $Pages > 1 ) { $pageLinks .= "$text_page "; for ($i = 1; $i <= $Pages; $i++) { if ( $i == $currPage ) { $pageLinks .= " $i "; } else { $pageLinks .= " $i " ; } } $pageLinks .= " $text_next "; } } print "
$text_results '$search_phrase'
"; $backLink = "$text_return"; print "$FMBlurb \n"; print "$pageLinks\n"; if ( $FoundMatch > 1 ) { print "$backLink
"; } print "$display
";
print "$pageLinks
";
if ( $FoundMatch > 1 ) { print "$backLink
"; }
?>






