Thanks to the fantastic youtube video you can get here if you want, I managed to create a php search engine. Below is the code:
<?php
foreach (array('questioncontent') as $varname) {
$questioncontent = (isset($_GET[$varname])) ? $_GET[$varname] : '';
}
?>
<form action="previousquestions.php" method="get">
<p>Search: <input type="text" name="questioncontent" value="<?php echo $questioncontent; ?>" /></p>
<p><input id="searchquestion" name="searchQuestion" type="submit" value="Search" /></p>
</form>
<?php
if (isset($_GET['searchQuestion'])) {
$searchquestion = $questioncontent;
$terms = explode(" ", $searchquestion);
$questionquery = "SELECT * FROM Question WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1){
$questionquery .= "QuestionContent LIKE '%$each%' ";
} else{
$questionquery .= "OR QuestionContent LIKE '%$each%' ";
}
}
$questionnum = mysql_num_rows($questionresult = mysql_query($questionquery));
else if($questionnum !=0){
$output = "";
$output .= "
<table border='1'>
<tr>
<th>Question</th>
</tr>
";
while ($questionrow = mysql_fetch_array($questionresult)) {
$output .= "
<tr>
<td>{$questionrow['QuestionContent']}</td>
</tr>";
}
$output .= " </table>";
echo $output;
}
}
mysql_close();
?>
Now I have a question about how, if possible, you can organize the search results. Let's say in the database I have these 3 entries:
My name
My name is Mayur Patel
My name is Patel
, "name Mayur Patel", , , , , , , , ( ), :
My name is Mayur Patel (all keywords match in this phrase)
My name is Patel (2 keywords match this phrase which are 'name' and 'Patel')
My name (1 keyword match this phrase which is 'name')
, , , ?