Now, we can create a script called search.php and actually do some searching. In this script, we will display a text box to allow for search terms to be entered. When the form is submitted, we will first do an exact keyword match. If that does not yield any results, we will perform a fuzzy search.
<?php <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Search</title> </head> <body> <h1>Search</h1> <p>Enter keywords to search for:</p> <form method=POST action="<?= $_SERVER['PHP_SELF'] ?>"> <p><input type="text" name="search_term" size="20"> <input type="submit" name="submit" value="Submit"></p> </form> <?php
require('searchclass.php');
if (isset ($_POST['submit'])) { $search = new Search($_POST['search_term']); $results = $search->doSearch(); if($results) { echo "<p><b>Your search results:</b></p>\n"; echo "<p>"; foreach($results as $row) { echo "<a href=\"{$row['url']}\">" . "{$row['url']}</a><br />\n"; } echo "</p>\n"; } else { $results = $search->doFuzzy(); echo "<p><b>No matches! "; if($results) { echo "These pages contain similar words " . "to what you searched for:</b></p>\n"; echo "<p><i>(Similar terms are in parentheses)</i></p>\n"; echo "<p>"; foreach($results as $row) { echo "<a href=\"{$row['url']}\">{$row['url']}" . "</a>&nbsp;({$row['keyword']})<br />\n"; } echo "</p>\n"; } else { echo "No similar words either.</b></p>\n"; } } } ?> </body> </html> ?>