A little help with a serch engine

Discussion in 'PHP' started by snowLee, Apr 16, 2009.

  1. #1
    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:
     
    snowLee, Apr 16, 2009 IP
  2. adstiger

    adstiger Peon

    Messages:
    409
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    what do you mean by between the dots? Do you mean to say the concatination?
     
    adstiger, Apr 16, 2009 IP
  3. snowLee

    snowLee Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    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.
     
    snowLee, Apr 16, 2009 IP
  4. jimbursch

    jimbursch Peon

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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/
     
    jimbursch, Apr 16, 2009 IP