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. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #21
    Replace <input type="" value="Search" /> with <input type="submit" value="Search" />.
     
    Arttu, Apr 23, 2012 IP
  2. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #22
    it was already <imput type="submit" value="Search" />
     
    Kurt Whittingham, Apr 23, 2012 IP
  3. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #23
    I meant replace <input type="" value="Search" /> with <input type="submit" value="Search" />.
    Also remember to prevent sql injection.
     
    Arttu, Apr 23, 2012 IP
  4. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #24
    now the search bar is just text again
     
    Kurt Whittingham, Apr 23, 2012 IP
  5. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #25
    Is it just me or does DP keep removing the word s-u-b-m-i-t (it did it again) from our messages? Add "or die(mysql_error())" and post the error.
     
    Arttu, Apr 23, 2012 IP
  6. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #26
    here is where i put it

    <form action="" method="get">
                <input type="text" name="kw" />
                <input type="" value="Search" />
                            
                </form>
                <?php
                or die(mysql_error())
                $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:
    Parse error: syntax error, unexpected T_LOGICAL_OR in /home/motorbik/public_html/new 2.php on line 47
     
    Kurt Whittingham, Apr 23, 2012 IP
  7. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #27
    I meant like this:
    $result = mysql_query($query) or die(mysql_error());

    Do you see the word "s-u-b-m-i-t" without dashes in the first message of this page? --never mind I just realized that it was the ad blocker on this computer that removed the word submit.
     
    Arttu, Apr 23, 2012 IP
  8. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #28
    yes this is what i see Replace <input type="" value="Search" /> with <input type="(S-U-B-M-I-T without dashes)" value="Search" />.
     
    Kurt Whittingham, Apr 23, 2012 IP
  9. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #29
    No error comes up... the search button is still just a text box tho
     
    Kurt Whittingham, Apr 23, 2012 IP
  10. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #30
    I tried it and it worked, maybe you just don't get any results with the keyword you tried?
    Change the button back to: <input type="submit" value="Search" />
     
    Arttu, Apr 23, 2012 IP
  11. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #31
    F**K ME thats annoying.... it wasnt working cos there was no keywords the same..... SORRY..

    is there a way for the search bar to search multiple fields?
     
    Kurt Whittingham, Apr 23, 2012 IP
  12. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #32
    also, how do i have an error message that say "the keywords "keywords they type" did not match any results" if there was no match..

    and i have another big question... once the other two questions are answered


    Thanks
     
    Kurt Whittingham, Apr 23, 2012 IP
  13. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #33
    
    $countQuery = "SELECT count(article_id) as count FROM articles WHERE keywords LIKE '%$keyword%' OR description LIKE '%$keyword%'";
    $countResult = mysql_query($queryQuery);
    $countArray = mysql_fetch_array($countResult);
    $count = $countArray['count'];
    
    if($count>0){
    echo $count. ' results found for the keyword ' .$keyword;
    }
    else{
    echo 'No results found for the keyword ' .$keyword;
    }
    
    PHP:
    SELECT * FROM articles WHERE keywords LIKE '%$keyword%' OR description LIKE '%$keyword%'
    Code (markup):
    Didn't test these but they should work.
     
    Arttu, Apr 23, 2012 IP
  14. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #34
    i got this warning

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/motorbik/public_html/new 2.php on line 55

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/motorbik/public_html/new 2.php on line 65

    This is the code
    <?php
                
                $keyword = $_GET['kw'];
                if(!empty($keyword)){
                require('connect.php');
    
                
                $countQuery = "SELECT count(article_id) as count FROM articles WHERE keyword LIKE '%$keyword%' OR description LIKE '%$keyword%'";
    $countResult = mysql_query($queryQuery);
    $countArray = mysql_fetch_array($countResult);
    $count = $countArray['count'];
    
    if($count>0){
    echo $count. ' results found for the keyword &nbsp;' .$keyword;
    }
    else{
    echo 'No results found for the keyword' .$keyword;
    }
                
                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
  15. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #35
    1. error:
    I didn't remember your field names correctly keyword should be keywords and description should be Description.
    2. error:
    You have to make 2 different queries to get the count and the results. So you shouldn't have removed anything from the code.
     
    Arttu, Apr 23, 2012 IP
  16. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #36
    So how would i set it out?
     
    Kurt Whittingham, Apr 25, 2012 IP
  17. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #37
    
    <?php
                
                $keyword = $_GET['kw'];
                if(!empty($keyword)){
                require('connect.php');
    
                
                $countQuery = "SELECT count(article_id) as count FROM articles WHERE keywords LIKE '%$keyword%' OR Description LIKE '%$keyword%'";
    $countResult = mysql_query($countQuery);
    $countArray = mysql_fetch_array($countResult);
    $count = $countArray['count'];
    
    if($count>0){
    echo $count. ' results found for the keyword &nbsp;' .$keyword;
    
    
                
    $query = "SELECT * FROM articles WHERE keywords LIKE '%$keyword%' OR Description 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 />";
    } 
    }
    else{
    echo 'No results found for the keyword' .$keyword;
    }
    
    
    }
    ?>
    
    PHP:
     
    Arttu, Apr 26, 2012 IP
  18. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #38
    hehe, cheers it works perfectly now.

    is there a way i can add the search bar next to the heading image, and the results show on the main area of the page
     
    Kurt Whittingham, Apr 26, 2012 IP
  19. Kurt Whittingham

    Kurt Whittingham Member

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #39
    How would i make it that when someone searches something it goes to a new page called "search" for instant?
     
    Kurt Whittingham, Apr 26, 2012 IP
  20. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #40
    Sure, it works the same way as static html would.
    Save the PHP code as search.php or something and change forms action attribute to search.php.
     
    Last edited: Apr 27, 2012
    Arttu, Apr 27, 2012 IP