Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Internal Search Engine tutorials

    I'm looking for any good tutorials on building a search function for a primarily database driven site. An article that has tips on how to get the most relevant content to display first is my primary concern.

    Thanks for any help!

  • #2
    Regular Coder
    Join Date
    Dec 2005
    Posts
    346
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Here is a very basic search box for your site. In the drop down menu make the Value of each entry the Column name in your MySQL DB. If have several DB's you want to search then just add another menu & make the value the table name. This isn't the best search script for your site, but it will get you started.

    PHP Code:
        <div align="center">
            <?PHP
            
    if($_POST['search1'])
            {
                 
    trim($_POST["searchterm"]);
                 if (!(
    $_POST["searchtype"]) || !($_POST["searchterm"]))
                 {
                    echo 
    "You have not entered search details. Please go back and try again.";
                    exit;
                 }
                
                
    $searchtype mysql_escape_string($_POST["searchtype"]);
                
    $searchterm mysql_escape_string($_POST["searchterm"]); 
                
                
    $query "SELECT * FROM table WHERE ".$searchtype." LIKE '%".$searchterm."%'";
                
    $result mysql_query($query);
                
    $num_results mysql_num_rows($result);
                
                echo 
    "<p>Number of entries found: ".$num_results."</p>";
                
                echo 
    '<TABLE border="0">';
                echo 
    '<TR><TH>First:</TH><TH>Last:</TH><TH>E-Mail</TH><TH>Phone</TH><TH>Phone 2</TH><TH>Street</TH>
                <TH>City</TH><TH>State</TH><TH>Zip</TH><TH>Country</TH></TR>'
    ;
                  
    $bgcolor "#FFFFFF";
                while ( 
    $row mysql_fetch_array$result ) )
                {
                    if (
    $bgcolor == "#FFFFFF"){
                        
    $bgcolor "#CACACA";
                    }else{
                        
    $bgcolor "#FFFFFF";
                    }
                    
                    echo 
    '<TR bgcolor="'.$bgcolor.'">';
                    echo 
    '<TD>' htmlspecialcharsstripslashes$row['id'] ) ) . '</TD>';
                    echo 
    '<TD>' htmlspecialcharsstripslashes$row['firstname'] ) ) .   '</TD>';
                    echo 
    '<TD>' htmlspecialcharsstripslashes$row['lastname'] ) ) .   '</TD>';
                    echo 
    '<TD>' htmlspecialcharsstripslashes$row['email'] ) ) .   '</TD>';
                    echo 
    '<TD>' htmlspecialcharsstripslashes$row['phone'] ) ) .   '</TD>';
                    echo 
    '<TD>' htmlspecialcharsstripslashes$row['phone2'] ) ) .   '</TD>';
                    echo 
    '<TD>' htmlspecialcharsstripslashes$row['street_address'] ) ) .   '</TD>';
                    echo 
    '<TD>' htmlspecialcharsstripslashes$row['city'] ) ) .   '</TD>';
                    echo 
    '<TD>' htmlspecialcharsstripslashes$row['state'] ) ) .   '</TD>';
                    echo 
    '<TD>' htmlspecialcharsstripslashes$row['postcode'] ) ) .   '</TD>';
                    echo 
    '<TD>' htmlspecialcharsstripslashes$row['country'] ) ) .   '</TD>';
                    echo 
    '</TR>';
                }
                
                echo 
    '</TABLE>';
            }
            else
            {
            
    ?>

            <h1><font face="Arial, Helvetica, sans-serif" color="#333333">Customer Search</font></h1>

            <form action="<?php $_SERVER['PHP_SELF'?>" method="post">
            <table width="40%" border="0">
              <tr>
                <td><div align="center"><font face="Arial, Helvetica, sans-serif" color="#333333" size="2">Choose Search Type:</font><br>
                    <select name="searchtype">
                      <option>Choose customer criteria</option>
                      <option value="id">Customer ID</option>
                      <option value="firstname">First Name</option>
                      <option value="lastname">Last Name</option>
                      <option value="email">E-Mail</option>
                      <option value="phone">Phone</option>
                      <option value="phone2">Mobile</option>
                      <option value="city">City</option>
                      <option value="state">State</option>
                      <option value="postcode">Zip</option>
                      <option value="country">Country</option>
                    </select>
    <br>
                </div></td>
                <td><div align="center"><font face="Arial, Helvetica, sans-serif" size="2" color="#333333">Enter Search Term:</font><br>
                        <input name="searchterm" type="text">
                </div></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td colspan="2"><div align="center">
                    <input name="search1" type=submit id="search1" value="Search">
                </div></td>
              </tr>
            </table>
                    </form>
            <?
            
    }
            
    ?> 
        
        </div>

  • #3
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,100
    Thanks
    2
    Thanked 23 Times in 23 Posts
    You might want to read this article, which walks you through how to build a basic site search engine. It includes relevancy of search results. Excellent stuff!
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!


  •  

    Posting Permissions

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