-
-
Save amnek0/f55f62d1340a6cea1ee6a38272ae82b2 to your computer and use it in GitHub Desktop.
Revisions
-
johnmorris created this gist
May 7, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,75 @@ <?php /** * Performs a search * * This class is used to perform search functions in a MySQL database * * @version 1.0 * @author John Morris <[email protected]> */ class search { /** * MySQLi connection * @access private * @var object */ private $mysqli; /** * Constructor * * This sets up the class */ public function __construct() { // Connect to our database and store in $mysqli property $this->connect(); } /** * Database connection * * This connects to our database */ private function connect() { $this->mysqli = new mysqli( 'localhost', 'root', 'root', 'snippets' ); } /** * Search routine * * Performs a search * * @param string $search_term The search term * * @return array/boolen $search_results Array of search results or false */ public function search($search_term) { // Sanitize the search term to prevent injection attacks $sanitized = $this->mysqli->real_escape_string($search_term); // Run the query $query = $this->mysqli->query(" SELECT title FROM search WHERE title LIKE '%{$sanitized}%' OR body LIKE '%{$sanitized}%' "); // Check results if ( ! $query->num_rows ) { return false; } // Loop and fetch objects while( $row = $query->fetch_object() ) { $rows[] = $row; } // Build our return result $search_results = array( 'count' => $query->num_rows, 'results' => $rows, ); return $search_results; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,51 @@ <?php //Check if search data was submitted if ( isset( $_GET['s'] ) ) { // Include the search class require_once( dirname( __FILE__ ) . '/class-search.php' ); // Instantiate a new instance of the search class $search = new search(); // Store search term into a variable $search_term = htmlspecialchars($_GET['s'], ENT_QUOTES); // Send the search term to our search class and store the result $search_results = $search->search($search_term); } ?> <!DOCTYPE html> <html> <head> <title>Search</title> </head> <body> <h1>Search</h1> <div class="search-form"> <form action="" method="get"> <div class="form-field"> <label for="search-field">Search</label> <input type="search" name="s" placeholder="Enter your search term..." results="5" value="<?php echo $search_term; ?>"> <input type="submit" value="Search"> </div> </form> </div> <?php if ( $search_results ) : ?> <div class="results-count"> <p><?php echo $search_results['count']; ?> results found</p> </div> <div class="results-table"> <?php foreach ( $search_results['results'] as $search_result ) : ?> <div class="result"> <p><?php echo $search_result->title; ?></p> </div> <?php endforeach; ?> </div> <div class="search-raw"> <pre><?php print_r($search_results); ?></pre> </div> <?php endif; ?> </body> </html>