Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Questions can be directed to http://gitlez.comxa.com/contact.php */
- /* Define Some Constants */
- defined('PHP_SELF')? NULL : define('PHP_SELF',$_SERVER['PHP_SELF']);
- /* Settings */
- $rows_per_page = 2;
- $cache_dir = 'search_caches/';
- $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
- $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.
- /* Variables */
- $search = (isset($_GET['search']))? strtolower(trim($_GET['search'])) : '';
- $page = (isset($_GET['page']) && ((int)$_GET['page']) !== 0)? (int)$_GET['page'] : 1;
- $cache_age = time() - ($cache_age * 60);
- $cache_key = sha1(strtolower($search));
- $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.
- /* Function: output */
- function output($content){
- Global $searchForm;
- echo '<html>
- <head>
- <title>Schools Search Database</title>
- <style type="text/css">
- .searchBox {
- text-align: center;
- }
- .bold {
- font-weight: bold;
- }
- .italic {
- font-style: italic;
- }
- #pagenation {
- text-align: center;
- border: 1px solid #CCC;
- padding: 8px;
- font-size: 19px;
- }
- </style>
- </head>
- <body>
- <h1>My Search Engine</h1>
- <div class="searchBox">
- ' . $searchForm . '
- </div>
- <hr>';
- echo $content;
- if(strlen($content) > 200){
- echo ' <hr>
- <div class="searchBox">
- ' . $searchForm . '
- </div>';
- }
- echo '</body>
- </html>';
- exit;
- }
- /* Caching Funcitons */
- function cache_exists($key){
- Global $cache_dir,$cache_age;
- $file = $cache_dir . $key . '.cache';
- return (file_exists( $file ) && filemtime($file) >= $cache_age);
- }
- function cached_array($key){
- Global $cache_dir;
- return unserialize(file_get_contents($cache_dir . $key . '.cache'));
- }
- function cache_array($key, $array){
- Global $cache_dir;
- return file_put_contents($cache_dir . $key . '.cache', serialize($array));
- }
- /* HTML in Variables */
- $searchForm = '
- <form method="get" action="' . PHP_SELF . '">
- <input type="text" size="90" name="search" value="' . $search . '">
- <input type="submit" value="Search">
- </form>' . PHP_EOL;
- /* Check for Cache Dir */
- if(!is_dir($cache_dir)){
- mkdir($cache_dir, 0666, true);
- }
- /* Get Search Results */
- if( empty($search)){
- // Default Page Message
- output('<h3>Welcome</h3>');
- }else if( strlen($search) === 1){
- output("<h3>Search Term is too short.</h3>");
- } else if (cache_exists($cache_key)){
- $all_results = cached_array($cache_key);
- }else{
- // This should be in another file, included here. With error checking
- $conn = mysql_connect( 'localhost', 'root', '');
- mysql_select_db( 'schools', $conn);
- $terms = array_diff(explode(' ', $search), $uselessWords);
- $x = 0;
- $stmt = "SELECT * FROM profession WHERE ";
- foreach( $terms as $term){
- if($x++ > 0){
- $stmt .= ' AND ';
- }
- $stmt .= "Keywords LIKE '%{$term}%'";
- }
- $all_results = Array();
- $results = mysql_query($stmt . ' LIMIT ' . $max_results);
- if($results){
- while($row = mysql_fetch_assoc($results)){
- $all_results[] = $row;
- }
- }
- if( count( $all_results) > 0){
- cache_array($cache_key, $all_results);
- }
- }
- /* Configure Results */
- $totResults = count($all_results);
- $totPages = ceil($totResults/$rows_per_page);
- // Check to make sure someone didn't put in a larger page num then exists.
- if($page > $totPages){
- $page = $totPages;
- }
- if($totResults === 0){
- $msg = '<h3>Sorry, there are no matching results for "<span class="italic">' . $search . '</span>"</h3>
- <ol>
- <li>Try more general words. For example: If you want to search "how to create a website"
- then use general keyword like <span class="italic">"create website"</span>.</li>
- <li>Try different words with similar meaning</li>
- <li>Please check your spelling</li>
- </ol>
- <br>';
- output($msg);
- }else{
- $msg = '<h3>' . $totResults . ' results found!</h3>';
- $results = array_splice($all_results, (($page - 1) * $rows_per_page), $rows_per_page);
- foreach($results as $row){
- $msg .= ' <div class="resultRow">
- <a href="' . $row['Url'] . '" class="bold">' . $row['Name'] . '</a>
- <br>
- ' . $row['Address'] . '
- <br>
- ' . $row['Fee'] . '
- <br>
- ' . $row['Telephone'] . '
- <br>
- ' . $row['Email'] . '
- <br>
- <a href="' . $row['Url'] . '">' . $row['Url'] . '</a>
- </div>' . PHP_EOL;
- }
- /* Pagenation */
- $pagenation = ' <div id="pagenation">' . PHP_EOL;
- $pgnItems = Array('search' => $search);
- // First & Prev
- if( $totPages > 1 && $page !== 1){
- $pgnItems['page'] = 1;
- $pagenation .= '<a href="' . PHP_SELF . '?' . http_build_query($pgnItems) . '">First</a> ' . PHP_EOL;
- $pgnItems['page'] = $page - 1;
- $pagenation .= '<a href="' . PHP_SELF . '?' . http_build_query($pgnItems) . '">Prev</a> ' . PHP_EOL;
- }else{
- $pagenation .= 'First Prev ' . PHP_EOL;
- }
- // Individual Pages.
- for($i=1;$i<=$totPages;++$i){
- if($i !== $page){
- $pgnItems['page'] = $i;
- $pagenation .= '<a href="' . PHP_SELF . '?' . http_build_query($pgnItems) . '">' . $i . '</a>' . PHP_EOL;
- }else{
- $pagenation .= '<span class="curPage">' . $i . '</span>' . PHP_EOL;
- }
- }
- // Last & Next
- if($totPages !== 1 && $page < $totPages){
- $pgnItems['page'] = $page + 1;
- $pagenation .= '<a href="' . PHP_SELF . '?' . http_build_query($pgnItems) . '">Next</a> ' . PHP_EOL;
- $pgnItems['page'] = $totPages;
- $pagenation .= '<a href="' . PHP_SELF . '?' . http_build_query($pgnItems) . '">Last</a> ' . PHP_EOL;
- }else{
- $pagenation .= 'Next Last ' . PHP_EOL;
- }
- $pagenation .= ' </div>' . PHP_EOL;
- output($msg . PHP_EOL . $pagenation);
- }
- ?>
Add Comment
Please, Sign In to add comment