1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Tutorial PHP Simple Search Engine

Discussion in 'PHP' started by deleted-account, Apr 19, 2010.

  1. #1
    In this tutorial we will be using a table with two rows title, and content. Our search script will be checking both rows for the searched terms. It will look like the following:

    search.php
    <?php
    $searchTerms = $_POST['query'];
    $query = mysql_query("SELECT * FROM posts WHERE title LIKE '%$searchTerms%' OR content LIKE '%$searchTerms%'");
    while ($post = mysql_fetch_array($query))
    {
      echo '<p><b>', $post['title'], '</b><br/>', $post['content'], '</p>';
    }
    if (mysql_num_rows($query) == 0)
    {
      echo '<p>No posts found!</p>';
    }
    ?>
    
    Code (markup):
    Believe it or not, that's it! Now I know it's very simple but it will get the job done. Now we must create our search form to place on our website.

    search.html
    <form action="search.php" method="post">
    <input type="text" name="query" id="query"/>
    <input type="submit" value="Search"/>
    </form>
    Code (markup):
    Now we are done, but there are a couple things you should consider to improve the search quality of your script. For starters currently we are not ordering the results in any way, generally you would want to display your posts in the order of popularity. That can easily be done by adding a row to your tables that counts how many times your post is viewed (stay tuned for our counter script). Another thing to think about is how you title your posts, be sure to spell correctly and include keywords that describe the document or it's topic.

    Congratulations you are now ready to have users finding content easier on your website. Thanks for reading and good luck with your sites!

    Copied from: Tutorial PHP Simple Search Engine
     
    Last edited: Apr 19, 2010
    deleted-account, Apr 19, 2010 IP
  2. digitalpointnet

    digitalpointnet Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What if , instead of a title
    think about there is an item list around 500items in the database, and the user should get suggestions of that list

    Do you know how to do this , i am sure that this involves javascript
     
    Last edited: Mar 22, 2011
    digitalpointnet, Mar 22, 2011 IP
  3. cipcip

    cipcip Well-Known Member

    Messages:
    1,935
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    160
    #3
    Instead:

    $searchTerms = $_POST['query'];
    Code (markup):
    You can use:

    $searchTerms = trim($_POST['query']);
    Code (markup):
    Or:

    $searchTerms = htmlspecialchars($_POST['query']);
    Code (markup):
    Basic security stuff :)
     
    cipcip, Mar 24, 2011 IP
  4. deleted-account

    deleted-account Active Member

    Messages:
    655
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    85
    #4
    You're going to have to elaborate on what exactly you want done. It would not take Javascript as this can all be done in PHP but I'm not entirely sure of what you're saying.
     
    deleted-account, Apr 2, 2011 IP
  5. lukefowell89

    lukefowell89 Peon

    Messages:
    182
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Bringing up a list of suggestions in a search bar while typing is done using Ajax. Everytime a key is pressed, it fires an event which in turn looks up in the database for items that match the currently entered value in the text field.

    E.g. if you are typing to word, 'football' by the time you get to 'foot' it will bring up for example Foot massage, Football, Foot Spa etc.

    When the Ajax is returned the PHP result, possible stored in a JSON format. You can then use javascript to loop through the results and put another item on the list of results.
     
    lukefowell89, Apr 5, 2011 IP
  6. deleted-account

    deleted-account Active Member

    Messages:
    655
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    85
    #6
    No sorry, JavaScript isn't exactly my forte.
     
    deleted-account, Apr 13, 2011 IP
  7. SametAras

    SametAras Well-Known Member

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    113
    #7
    You had better use FULLTEXT for search. You don't believe users besides you must filter for POST or GET.

    Here are functions: mysql_real_escape_string() and strip_tags()
     
    SametAras, Apr 13, 2011 IP