I have a search engine that is working , but only when I search 1 word. Whenever I search multiple keywords I only get 1 result.
Example : In my database I have tags like 'test' and 'hello' ; Whenever I enter "test hello" and click "Search" it displays :
1 result hello (this being the title of the post with the tag = hello).
My code (search.php - the page where I get the search results):
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if(!$button) {
echo "you didn't submit a keyword";
} else {
if(strlen($search)<=1) {
echo "Search term too short";
} else {
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","root");
mysql_select_db("myschool");
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each) {
$x = NULL;
$construct = NULL;
$x++;
if($x==1) {
$construct .="tag LIKE '%$search_each%'";
} else {
$construct .="OR tag LIKE '%$search_each%'";
}
$construct ="SELECT * FROM posts WHERE $construct";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum==0) {
echo "Sorry, there are no matching result for <b>$search</b>.";
} else {
echo "$foundnum results found !<p>";
while($runrows = mysql_fetch_assoc($run)) {
$title = $runrows ['title'];
$tag = $runrows ['tag'];
echo "<a href='#'><b>$title</b></a><br>";
}
}
}
}
}
?>
Problem is probably around the $x=++
part , because I believe the engine is not displaying or even searching through all the rows in the database , and not displaying when the num row count > 1 .
Thanks in advance guys.
EDIT :
I now get multiple results with the code above BUT I get them in this form :
You searched for hello test postare
1 results found ! HELLO 1 results found !
Test 1 results found !
postare noua
How can I make it add the results in 1 place , and not say it everytime it finds a new result for a different keyword ?