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.

How do i make a search bar that searches through my website?

Discussion in 'Programming' started by Kurt Whittingham, Apr 17, 2012.

  1. #1
    I want a search bar that will search through my databases in MySQL..

    I have a database with multiple tables inside...

    the data base is 'categ'

    and the tables are
    • supercross_race_reviews
    • how_to
    • Gear_reviews
    • kurt_blog
    • cameron_blog

    I want it to be if someone searches "Boots"
    then all articles that contain "boots" will show up


    Im using php.. but im really new to it..

    Thanks Kurt
     
    Kurt Whittingham, Apr 17, 2012 IP
  2. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #2
    What kind of structure do the tables have?
     
    Arttu, Apr 17, 2012 IP
  3. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #3

    What do you mean sorry?
     
    Kurt Whittingham, Apr 17, 2012 IP
  4. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #4
    Can you list the fields in your "how_to" table.
     
    Last edited: Apr 18, 2012
    Arttu, Apr 18, 2012 IP
  5. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #5
    This is the fields for the how_to table
    Id
    Url
    Description
    Author

    But the other tables have different fields.
    [TABLE="class: data"]
    [TR="class: even"]
    [TH][/TH]
    [TH][/TH]
    [/TR]
    [/TABLE]
     
    Kurt Whittingham, Apr 18, 2012 IP
  6. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #6
    Here's the example I gave you in your last thread modified to work with your how_to table.
    
    <form action="" method="get">
    <input type="text" name="kw" />
    <input type="" value="Search" />
    </form>
    <?php
    $keyword = $_GET['kw'];
    if(!empty($keyword)){
    	mysql_connect("localhost", "username", "password");
    	mysql_select_db("dbname");
    
    	
    	$query = "SELECT * FROM how_to WHERE Description LIKE '%$keyword%'";
    	$result = mysql_query($query);
    	
    	while($row = mysql_fetch_array($result)) {
    		echo $row['url']. "<br />";
    	} 
    }
    ?>
    
    PHP:
     
    Arttu, Apr 18, 2012 IP
  7. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #7

    the bar doesnt work... it has a type bar and then the search bar is just a box with search in it
     
    Kurt Whittingham, Apr 18, 2012 IP
  8. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #8
    Sorry about that.
    Change
    <input type="" value="Search" />
    to
    <input type="submit" value="Search" />
     
    Arttu, Apr 18, 2012 IP
  9. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #9
    THANK YOU THANK YOU THANK YOU!!!! Dude this works thanks heaps you have no idea how grateful i am..

    quick question would i be smart for me to put a "Keywords" field in and have the search bar search the "keywords"

    also how do i get it to search all the table(sorry)
     
    Kurt Whittingham, Apr 18, 2012 IP
  10. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #10
    Yes.
    Well... You could use UNION, but you have to make sure that you select the same amount of fields from all of the tables.
    For example:
    SELECT id,url FROM how_to WHERE Description LIKE '%$keyword%'
    UNION
    SELECT id,url FROM Gear_reviews WHERE Description LIKE '%$keyword%'
    UNION
    SELECT id,url FROM supercross_race_reviews WHERE Description LIKE '%$keyword%'
    Code (markup):
    If you use SELECT * and the tables have a different amount of fields in them it wont work.

    Personally I would have made the database structure different:
    eg.
    categories:
    -id
    -name
    articles:
    -id
    -category_id
    -description
    -url
    -author

    Then you could have just used a query like this and would not have to modify your code every time you add a new category.
    
    SELECT * FROM articles WHERE description LIKE '%$keyword%'
    
    Code (markup):
     
    Last edited: Apr 19, 2012
    Arttu, Apr 19, 2012 IP
  11. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #11
    im a little confused.

    so the table name would be categories?

    then whats the id, and name for??

    then the articles?
     
    Kurt Whittingham, Apr 19, 2012 IP
  12. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #12
    Instead of having following tables:
    supercross_race_reviews
    how_to
    Gear_reviews
    kurt_blog
    cameron_blog
    you would only have 2 tables named "categories" and "articles". Then you would insert how_to, gear_reviews, etc... to the categories table.
    So you would have something like this:
    
    category_id	|name
    --------------------------------
    1		|supercross_race_reviews 
    2		|how_to
    3		|Gear_reviews
    4		|kurt_blog
    5		|cameron_blog
    
    Code (markup):
    and articles table would be something like this:
    
    article_id	|category_id	|description	                                     |url	|author
    ---------------------------------------------------------------------------------------------------------
    1		|3		|some article in Gear_reviews category               |urlhere	| 	author
    2		|2		|some article in how_to category                     |urlhere	| 	author
    
    Code (markup):
    Based on your database name "categ" I assume that you have other databases too?
     
    Arttu, Apr 19, 2012 IP
  13. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #13
    That seems like a much better set up.. i will re do it and change it to this...

    and yeah i do but dont use them
     
    Kurt Whittingham, Apr 19, 2012 IP
  14. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #14
    i just need a little help.. this is what i had before.. it would take info from the database and show it on a page.. but now i dont know how to get it

    <?php require('connect.php');
        
    
        $result = mysql_query("SELECT * FROM supercross_race_reviews ORDER BY id DESC") 
        or die(mysql_error());  
        
        echo "<table width='100%' cellspacing='10'>";
        // keeps getting the next row until there are no more to get
        while($row = mysql_fetch_array( $result )) {
        // Print out the contents of each row into a table
        echo "<tr><td>"; 
        // arrow
        echo ("<img src='arrow.gif'>"); echo "&nbsp;&nbsp;";
        // article name
        echo '<a style="font-size: 25px" href="'. $row['url'] .'">'. $row['article_name'] .'</a>';
        echo "</td></tr><tr><td>"; 
        // author + date
        echo $row['author']; echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; echo $row['date'];
        echo "</td></tr><tr><td>";
        // Description
        echo "Brief Decription : "; echo "&nbsp;&nbsp;"; echo $row['description'];
        echo "<hr /></td></tr>";
    }
        ?>
    PHP:
     
    Kurt Whittingham, Apr 20, 2012 IP
  15. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #15
    Change the query to:
    SELECT * FROM articles WHERE category_id='{REPLACE-THIS-WITH-ID}' ORDER BY article_id DESC
    Code (markup):
    If I were you I would use something like this:
    
    <?php
    $category_id = intval($_GET['id']);
    
    require('connect.php');
    
    $result = mysql_query("SELECT * FROM categories WHERE category_id='$category_id'") 
        or die(mysql_error());  
    
    $categoryData = mysql_fetch_array($result);
    if(!$categoryData){
    	die("Invalid id");
    }
    echo '<h1>' .$categoryData['name']. '</h1>';
    
    
    
    
        $result = mysql_query("SELECT * FROM articles WHERE category_id='$category_id' ORDER BY article_id DESC") 
        or die(mysql_error());  
        
        echo "<table width='100%' cellspacing='10'>";
        // keeps getting the next row until there are no more to get
        while($row = mysql_fetch_array( $result )) {
        // Print out the contents of each row into a table
        echo "<tr><td>"; 
        // arrow
        echo ("<img src='arrow.gif'>"); echo "&nbsp;&nbsp;";
        // article name
        echo '<a style="font-size: 25px" href="'. $row['url'] .'">'. $row['article_name'] .'</a>';
        echo "</td></tr><tr><td>"; 
        // author + date
        echo $row['author']; echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; echo $row['date'];
        echo "</td></tr><tr><td>";
        // Description
        echo "Brief Decription : "; echo "&nbsp;&nbsp;"; echo $row['description'];
        echo "<hr /></td></tr>";
    }
    ?>
    
    PHP:
     
    Arttu, Apr 20, 2012 IP
  16. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #16
    What is this for?
    <?php
    $category_id = intval($_GET['id']);
    
    require('connect.php');
    
    $result = mysql_query("SELECT * FROM categories WHERE category_id='$category_id'") 
        or die(mysql_error());  
    
    $categoryData = mysql_fetch_array($result);
    if(!$categoryData){
        die("Invalid id");
    }
    echo '<h1>' .$categoryData['name']. '</h1>';
    PHP:
    i tried the edited stuff you gave me and its saying "Table 'motorbik_categ.categories' doesn't exist"
     
    Kurt Whittingham, Apr 21, 2012 IP
  17. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #17
    That would get the name of the category, but it seems that you didn't create the categories table.
     
    Arttu, Apr 23, 2012 IP
  18. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #18
    i dont need it! is it okay for me to get rid of the top part
     
    Kurt Whittingham, Apr 23, 2012 IP
  19. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #19
    yes (this is here just because my message was too short)
     
    Arttu, Apr 23, 2012 IP
  20. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #20
    okay thankyou it works now... but the search bar does not... it was working but now its not

    <form action="" method="get">
                <input type="text" name="kw" />
                <input type="submit" value="Search" /> 
                </form>
                <?php
                $keyword = $_GET['kw'];
                if(!empty($keyword)){
                require('connect.php');
    
                
                $query = "SELECT * FROM articles WHERE keywords LIKE '%$keyword%'";
                $result = mysql_query($query);
                
                while($row = mysql_fetch_array($result)) {
                    echo ("<img src='arrow.gif'>"); echo "&nbsp;&nbsp;";
                    echo '<a style="font-size: 20px" href="'. $row['url'] .'">'. $row['article_name'] .'</a>';
                    echo "<br />";
                } 
                }
                ?>
    PHP:
     
    Kurt Whittingham, Apr 23, 2012 IP