gitlez

Simple Search Engine - Answer To A Yahoo Answers Question

Jul 11th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.69 KB | None | 0 0
  1. <?php
  2.  
  3. /* Questions can be directed to http://gitlez.comxa.com/contact.php */
  4.  
  5.  
  6. /*    Define Some Constants    */
  7. defined('PHP_SELF')? NULL : define('PHP_SELF',$_SERVER['PHP_SELF']);
  8.  
  9.  
  10. /*    Settings    */
  11. $rows_per_page = 2;
  12. $cache_dir = 'search_caches/';
  13. $cache_age = 10; // Time (in minutes) for the max age of a result. This really should be at least an hour or so, unless you update your site often
  14. $max_results = 100; // The maximum number of results a search may produce. A thousand search results is useless, because a user is not going to go through 1000 results.
  15.  
  16. /*    Variables    */
  17. $search = (isset($_GET['search']))? strtolower(trim($_GET['search'])) : '';
  18. $page = (isset($_GET['page']) && ((int)$_GET['page']) !== 0)? (int)$_GET['page'] : 1;
  19. $cache_age = time() - ($cache_age * 60);
  20. $cache_key = sha1(strtolower($search));
  21. $uselessWords = Array('a','about','all','an','and','are','as','at','be','been','but','for','from','has','have','how','in','is','it','its','of','only','the','them','they','this','to','was','what','who','with'); // Words to be removed from the search terms.
  22.  
  23. /*    Function: output    */
  24. function output($content){
  25.     Global $searchForm;
  26.     echo '<html>
  27.    <head>
  28.        <title>Schools Search Database</title>
  29.        <style type="text/css">
  30.            .searchBox {
  31.                text-align: center;
  32.            }
  33.            .bold {
  34.                font-weight: bold;
  35.            }
  36.            .italic {
  37.                font-style: italic;
  38.            }
  39.            #pagenation {
  40.                text-align: center;
  41.                border: 1px solid #CCC;
  42.                padding: 8px;
  43.                font-size: 19px;
  44.            }
  45.        </style>
  46.    </head>
  47. <body>
  48.    <h1>My Search Engine</h1>
  49.    <div class="searchBox">
  50.        ' . $searchForm . '
  51.    </div>
  52.    <hr>';
  53.     echo $content;
  54.     if(strlen($content) > 200){
  55.         echo '    <hr>
  56.    <div class="searchBox">
  57.        ' . $searchForm . '
  58.    </div>';
  59.     }
  60.     echo '</body>
  61. </html>';
  62.     exit;
  63. }
  64. /*    Caching Funcitons    */
  65. function cache_exists($key){
  66.     Global $cache_dir,$cache_age;
  67.     $file = $cache_dir . $key . '.cache';
  68.     return (file_exists( $file ) && filemtime($file) >= $cache_age);
  69. }
  70. function cached_array($key){
  71.     Global $cache_dir;
  72.     return unserialize(file_get_contents($cache_dir . $key . '.cache'));
  73. }
  74. function cache_array($key, $array){
  75.     Global $cache_dir;
  76.     return file_put_contents($cache_dir . $key . '.cache', serialize($array));
  77. }
  78.  
  79. /*    HTML in Variables    */
  80. $searchForm = '
  81.        <form method="get" action="' . PHP_SELF . '">
  82.            <input type="text" size="90" name="search" value="' . $search . '">
  83.            <input type="submit" value="Search">
  84.        </form>' . PHP_EOL;
  85.        
  86. /*    Check for Cache Dir    */
  87. if(!is_dir($cache_dir)){
  88.     mkdir($cache_dir, 0666, true);
  89. }
  90.  
  91. /*    Get Search Results    */
  92. if( empty($search)){
  93.     // Default Page Message
  94.     output('<h3>Welcome</h3>');
  95. }else if( strlen($search) === 1){
  96.     output("<h3>Search Term is too short.</h3>");
  97. } else if (cache_exists($cache_key)){
  98.     $all_results = cached_array($cache_key);
  99. }else{
  100.     // This should be in another file, included here. With error checking
  101.     $conn = mysql_connect( 'localhost', 'root', '');
  102.     mysql_select_db( 'schools', $conn);
  103.    
  104.     $terms = array_diff(explode(' ', $search), $uselessWords);
  105.     $x = 0;
  106.     $stmt = "SELECT * FROM profession WHERE ";
  107.     foreach( $terms as $term){
  108.         if($x++ > 0){
  109.             $stmt .= ' AND ';
  110.         }
  111.         $stmt .= "Keywords LIKE '%{$term}%'";
  112.     }
  113.  
  114.     $all_results = Array();
  115.     $results = mysql_query($stmt . ' LIMIT ' . $max_results);
  116.     if($results){
  117.         while($row = mysql_fetch_assoc($results)){
  118.             $all_results[] = $row;
  119.         }
  120.     }
  121.     if( count( $all_results) > 0){
  122.         cache_array($cache_key, $all_results);    
  123.     }
  124. }
  125.  
  126. /*    Configure Results    */
  127. $totResults = count($all_results);
  128. $totPages = ceil($totResults/$rows_per_page);
  129.  
  130. // Check to make sure someone didn't put in a larger page num then exists.
  131. if($page > $totPages){
  132.     $page = $totPages;
  133. }
  134.  
  135.  
  136. if($totResults === 0){
  137.     $msg = '<h3>Sorry, there are no matching results for "<span class="italic">' . $search . '</span>"</h3>
  138.        <ol>
  139.            <li>Try more general words. For example: If you want to search "how to create a website"
  140.        then use general keyword like <span class="italic">"create website"</span>.</li>
  141.        <li>Try different words with similar meaning</li>
  142.        <li>Please check your spelling</li>
  143.        </ol>
  144.        <br>';
  145.     output($msg);
  146. }else{
  147.     $msg = '<h3>' . $totResults . ' results found!</h3>';
  148.     $results = array_splice($all_results, (($page - 1) * $rows_per_page), $rows_per_page);
  149.     foreach($results as $row){
  150.         $msg .= '        <div class="resultRow">
  151.            <a href="' . $row['Url'] . '" class="bold">' . $row['Name'] . '</a>
  152.            <br>
  153.            ' . $row['Address'] . '
  154.            <br>
  155.            ' . $row['Fee'] . '
  156.            <br>
  157.            ' . $row['Telephone'] . '
  158.            <br>
  159.            ' . $row['Email'] . '
  160.            <br>
  161.            <a href="' . $row['Url'] . '">' . $row['Url'] . '</a>
  162.        </div>' . PHP_EOL;
  163.     }
  164.     /*    Pagenation    */
  165.     $pagenation = '        <div id="pagenation">' . PHP_EOL;
  166.     $pgnItems = Array('search' => $search);
  167.     // First & Prev
  168.     if( $totPages > 1 && $page !== 1){
  169.         $pgnItems['page'] = 1;
  170.         $pagenation .= '<a href="' . PHP_SELF . '?' . http_build_query($pgnItems) . '">First</a> ' . PHP_EOL;
  171.         $pgnItems['page'] = $page - 1;
  172.         $pagenation .= '<a href="' . PHP_SELF . '?' . http_build_query($pgnItems) . '">Prev</a> ' . PHP_EOL;
  173.     }else{
  174.         $pagenation .= 'First Prev ' . PHP_EOL;
  175.     }
  176.     // Individual Pages.
  177.     for($i=1;$i<=$totPages;++$i){
  178.         if($i !== $page){
  179.             $pgnItems['page'] = $i;
  180.             $pagenation .= '<a href="' . PHP_SELF . '?' . http_build_query($pgnItems) . '">' . $i . '</a>' . PHP_EOL;
  181.         }else{
  182.             $pagenation .= '<span class="curPage">' . $i . '</span>' . PHP_EOL;
  183.         }
  184.     }
  185.     // Last & Next
  186.     if($totPages !== 1 && $page < $totPages){
  187.         $pgnItems['page'] = $page + 1;
  188.         $pagenation .= '<a href="' . PHP_SELF . '?' . http_build_query($pgnItems) . '">Next</a> ' . PHP_EOL;
  189.         $pgnItems['page'] = $totPages;
  190.         $pagenation .= '<a href="' . PHP_SELF . '?' . http_build_query($pgnItems) . '">Last</a> ' . PHP_EOL;
  191.     }else{
  192.         $pagenation .= 'Next Last ' . PHP_EOL;
  193.     }
  194.     $pagenation .= '        </div>' . PHP_EOL;
  195.    
  196.     output($msg . PHP_EOL . $pagenation);
  197. }
  198. ?>
Add Comment
Please, Sign In to add comment