I want to build a search engine for my site. I have the code that brings me from the database only the title that has the keywords that I searched for. Can someone, please, give a help with what I have to put next in my code to bring from the database and the content with dots between searched words like a normal search engine? This is my code so far: <?php include_once("mysql_connect.php"); if(isset($_POST['submit'])){ $problem = FALSE; if(empty($_POST['search'])){ $problem = TRUE; echo "<p style=\"color:#FF0000;\">Please enter a search term!</p>"; } if(!$problem){ $text = $_POST['search']; $query = "SELECT * from news_posts WHERE title LIKE '%$text%' OR post LIKE '%$text%' OR author LIKE '%$text%'"; $result = @mysql_query($query); $query2 = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts ORDER BY id DESC limit 15"; $result2 = @mysql_query($query); if($result && $result2){ echo "<h1>found " . mysql_num_rows($result) . " results</h1><br />"; echo "<ul>"; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $mess=$row['post']; $view = 'page.php?id=' . $row['id']; echo '<li>' . $row['id'] . ' - <a href="' . $view . '">' . $row['title'] . '</a> - ' . $row['author'] . '</li>'; } echo "</ul><br /> <a href=\"search.php\">Search Again?</a>"; } else { echo "No posts matched your query"; } } } else { ?> <form id="search" action="search.php?search" method="post"> Keyword: <input type="text" name="search" size="30" class="text" /><br /> <input type="submit" name="submit" value="Search" /> </form> <?php } ?> PHP:
I mean If php finds in the database one word in the more than one sentence to print on the screen a few words from every sentence that has the searched word, with dots between the sentences.
You can use substr() to select an x number of characters in the sentence. us2.php.net/manual/en/function.substr.php Or I found this function to select x number of words: tiffanybbrown.com/2009/03/26/php-quickie-select-a-certain-number-of-words-from-a-string/