Php search with get method

Discussion in 'PHP' started by venardv, Jun 4, 2009.

  1. #1
    Does anyone know how to make a search with the get method? Or know a place I can learn it from? I want to do one for my blog.
     
    venardv, Jun 4, 2009 IP
  2. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Something to this nature. Add db connection commands.. and change to your
    structure. and thats the basics of it

    
    <?php
    // ADD YOUR DATABASE CONNECTION STUFF HERE.
    
    
    
    $term = $_GET['term'];
    $query = "SELECT * FROM `TABLE` WHERE `Value` = '".mysql_real_escape_string($term)."'";
    $result = mysql_query($query);
    while($row = mysql_fetch_assoc($result))
    {
    echo $row['name'];
    echo $row['column name'];
    echo $row['other stuff'];
    }
    ?>
    
    PHP:
     
    atlantaazfinest, Jun 4, 2009 IP
  3. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you want more leeway in what you're searching for, using % around the variable. ->SELECT * FROM table WHERE column LIKE %$term% OR othercolumn LIKE %$term%

    And escape it from bad characters, of course.
     
    SHOwnsYou, Jun 4, 2009 IP
  4. buldozerceto

    buldozerceto Active Member

    Messages:
    1,137
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    88
    #4
    Or you can use mysql fulltext search. Google mysql fulltext tutorial
     
    buldozerceto, Jun 4, 2009 IP
  5. venardv

    venardv Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    I've made my search and it works fine, but I want to hide the blogs that are showing, so that they only show when you search for them. www.venardveloria.com/Search.php
    Please help.
     
    venardv, Jun 4, 2009 IP
  6. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You only want to echo your blog posts after you've search.

    
    <form method="post" action="<?php $_SERVER[PHP_SELF]; ?>">
    <input type="text" name="search" size=25 maxlength=25>
    <input type="Submit" name="Submit" value="Search">
    </form>
    
    
    <?php
    if (isset($_POST['Submit']))
    {
    $result = mysql_query("SELECT * FROM table WHERE column LIKE %search%");
    while ($row = mysql_fetch_array($result))
    {
    echo all your data from your blog posts
    }
    }
    ?>
    
    PHP:
     
    SHOwnsYou, Jun 5, 2009 IP
  7. venardv

    venardv Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    Thanks SHOwnsYou!
     
    venardv, Jun 5, 2009 IP
  8. venardv

    venardv Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8
    What about errors, like if the search term wasn't found? Where and how do I place the die statement?

    And how can I order the items that were found by date?

    Sorry for the troubles, I don't know much PHP.
     
    venardv, Jun 7, 2009 IP
  9. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    This part I'm not sure of. But I think it will work.
    
    <form method="post" action="<?php $_SERVER[PHP_SELF]; ?>">
    <input type="text" name="search" size=25 maxlength=25>
    <input type="Submit" name="Submit" value="Search">
    </form>
    
    
    <?php
    if (isset($_POST['Submit']))
    {
    $result = mysql_query("SELECT * FROM table WHERE column LIKE %search% ORDER BY date");
    if (!$result)
    {
    echo "Sorry. No posts were found that matched your search query.";
    }
    else
    {
    while ($row = mysql_fetch_array($result))
    {
    echo all your data from your blog posts
    }
    }
    }
    ?>
    
    PHP:
     
    SHOwnsYou, Jun 7, 2009 IP