Results 1 to 4 of 4

Thread: PHP Search

  1. #1
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default PHP Search

    Hi,

    I am looking for a way to have my visitors search using a search bar on my site. Depending on the keywords used, i would like to "include" a specific page.

    Is there any way of doing this?

    If so, I want to manually make a list of all the keyword(s) and which page to include.

    Has anybody done something like this before?

  2. #2
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Ok, I've changed a few things now. I am now using a mySQL database, and would like to use the database's search function, but how would i go about doing this?

    I would like the users to type their search querie into a search box, and then show the data matching the querie...

    I dont know the html code to put a search box / form, or the mySQL statement thingy to use! Can anyone help?

    PS. The search queire is part of the url, correct? e.g. search.php?q=keywprd ?

  3. #3
    Join Date
    May 2005
    Location
    Hawaii
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I wrote this, I am still new to php so It might not be correct, but I works. Feel free to edit it and use it.
    PHP Code:
    <?php
    if($_POST['action'] == "search") {
    if(
    strlen($_POST['keyword']) <= 3){
        print 
    "<p><b>Your keyword must be longer than 2 characters</b><br>";
    }
    else{
    // Database Connection
    // Create database connection and select database
    mysql_select_db('database'mysql_pconnect('server','username','password')) or die (mysql_error()); 

    // Time Fuction
    $time microtime(); 
    $time number_format($time,3); 


    // Perform MySQL query on only the current page number's results
    $keyword "".$_GET['q']."";
    $query_tbl = ("SELECT * FROM search_data WHERE `keywords` LIKE CONVERT( _utf8 '%".$keyword."%'
    USING latin1 )"
    );
    $sql mysql_query($query_tbl);
    if (
    mysql_num_rows($sql) == true){
    echo 
    "<p><b>",mysql_affected_rows(),"</b> Search Result(s) Found For <b>$keyword</b></p></font>";
    while(
    $row mysql_fetch_array($sql)){
        
    // Build your formatted results here.
        
    echo "<li><a href=\"".$row['link']."\"><b>".$row['title']."</b></a><br>".$row['discription']."<br><font color=\"gray\"><small>".$row['link']."</small></font><br><br>";
    }
    echo 
    "<br>Search executed in <b>$time</b> seconds.<br><br>";
    // Figure out the total number of results in DB:
    $total_results mysql_result(mysql_query("SELECT COUNT(*) as Num FROM search_data "),0);

    ?>
    <form action="<? echo ($_SERVER['PHP_SELF']); ?>" method="get">
        Search Again:
                <input type="text" name="q" size="20" maxlength="100">
                <input type="submit" name="submit" value="Search">
                <input type ="hidden" name="action" value ="search">
        </form>
    <?php    
    }
    if (
    mysql_num_rows($sql) == false){
            echo 
    "<p>Sorry but we could not find any results for <b>$keyword.</b><br><br>";
                
                echo 
    "Search executed in <b>$time</b> seconds.<br>";
    ?>
    <form action="<? echo ($_SERVER['PHP_SELF']); ?>" method="get">
        Search Again:
                <input type="text" name="q" size="20" maxlength="100">
                <input type="submit" name="submit" value="Search">
                <input type ="hidden" name="action" value ="search">
        </form>
    <?php
    }
    }
    }else if(!
    $action || $action == ""){
    ?>
    <table border="0" width="274" cellspacing="1" cellpadding="3">
                    <tr>
                      <td width="100%" style="text-align:justify"><b>Search</b>:</td>
                    </tr>
                    <tr>
                      <td width="100%"><form action="<? echo ($_SERVER['PHP_SELF']); ?>" method="get">
                          <input type="text" name="q" size="30" maxlength="175">
                          <input type="submit" name="submit" value="Search">
                          <input type ="hidden" name="action" value ="search">
                      </form></td>
                    </tr>
                    <tr>
                      <td></td>
                    </tr>
                  </table>
    <?php 
    }
    ?>
    Last edited by dead-poetic; 11-11-2005 at 01:00 AM.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Seems mostly correct - haven't checked it in any great depth.
    A few minor pointers:
    Code:
    <? echo ($_SERVER['PHP_SELF']); ?>
    You've used the short opening tag (<?), assuming that the target server has this enabled. Firstly, if you're going to make that assumption, you could have used the "short echo" tag:
    Code:
    <?=$_SERVER['PHP_SELF']?>
    Secondly, and preferably, you shouldn't really use short tags at all in code that will be run on an unknown server.
    Code:
    if (mysql_num_rows($sql) == false) {
    if automatically checks for a boolean value. There is no need to use a comparison in order to return a boolean from this function, which obviously already returns a boolean.
    Code:
    if (!mysql_num_rows($sql)) {
    would be preferable.
    Code:
    $keyword = "".$_GET['q']."";
    No idea what you're trying to do with that. If it was just one null string you were concatenating to it, I'd say you were trying to convert a non-string value to a string. If this is what you were trying to do, remember that all the values in $_GET and $_POST are already strings.
    mysql_pconnect('server','username','password')
    Using *_pconnect() as opposed to *_connect() has several advantages, and several disadvantages. For readers who don't know the difference: the _connect() functions establish a connection for the duration of the script's runtime, or until they are close()ed. The _pconnect() functions create a "persistent" connection, if none exists, which will then be picked up by future _pconnect() calls. However, this requires memory on the server, for so long as the connection exists. It is thus more efficient only in situations in which the connection will be used extensively. I'd generally say (as an estimate) that a hundred connections per hour warrants a _pconnect(). For less intensive usage, _connect() will yield better performance. Your usage here isn't necessarily a bad thing, but I tend to assume a lower number of requests and use _connect().
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •