search form to make a search form from the search script

Discussion in 'PHP' started by lazarus, Mar 31, 2006.

  1. #1
    i use a script to search my database and i want to know how i can instead of using one page i want to make it so that the home page will have a text box and it can search and display result on a next page. if someone can help me thank you. HERES THE SCRIPT.....


    
    <html>
    <head><title>Search</title></head>
    <body bgcolor="#000099">
    <body>
    <p><center><h2>
    !Search Our Database!</h2>
      <p>&nbsp;</p>
    </center></p>
    
    <?php
    // Full-Text Search Example
    // Conect to the database.
    $cnx = mysql_connect('******', '&^^^^', 'questsolutions') or die ("Could not connect");
    mysql_select_db('^^^^^^',  $cnx) or die (mysql_error());
    
    // Create the search function:
    
    function searchForm()
    {
      // Re-usable form
      
      // variable setup for the form.
      $searchwords = (isset($_GET['words']) ? htmlspecialchars(stripslashes($_REQUEST['words'])) : '');
      $all = (($_GET['mode'] == 'all') ? ' selected="selected"' : '' );
      $company = (($_GET['mode'] == 'company') ? ' selected="selected"' : '' );
      $city = (($_GET['mode'] == 'city') ? ' selected="selected"' : '' );
      $state = (($_GET['mode'] == 'state') ? ' selected="selected"' : '' );
      $country = (($_GET['mode'] == 'country') ? ' selected="selected"' : '' );
      $sic = (($_GET['mode'] == 'sic') ? ' selected="selected"' : '' ); 
      $keywords= (($_GET['mode'] == 'keywords') ? ' selected="selected"' : '' ); 
      
      echo '<form method="get" action="'.$_SERVER['PHP_SELF'].'">';
      echo '<input type="hidden" name="cmd" value="search" />';
      echo 'Search for: <input type="text" name="words" value="'.$searchwords.'" /> ';
      echo 'Mode: ';
      echo '<select name="mode">';
      echo '<option value="all"'.$normal.'>All</option>';
      echo '<option value="company"'.$company.'>Company</option>';
      echo '<option value="city"'.$city.'>City</option>';
      echo '<option value="state"'.$state.'>State</option>';
      echo '<option value="country"'.$country.'>Country</option>';
      echo '<option value="sic"'.$sic.'>SIC</option>';
      echo '<option value="keywords"'.$keywords.'>Keywords</option>';
      echo '</select> ';
      echo '<input type="submit" value="Search" />';
      echo '</form>';
    }
    
    
    // Create the navigation switch
    $cmd = (isset($_GET['cmd']) ? $_GET['cmd'] : '');
    
    switch($cmd)
    {
      default:
        echo '<h1></h1>';
        searchForm();
      
      break;
      
      
      case "search":
        searchForm();
        echo '<h3>Search Results:</h3><br />';
        
        $searchstring = mysql_escape_string($_GET['words']);
        switch($_GET['mode'])
        {
          case "all":
    
            $sql = "SELECT *, 
                  MATCH(companyName, address, city, state, country, phoneNumber, contact1, sic, faxNumber, keyWords) 
                  AGAINST ('$searchstring') AS score FROM contacts
                  WHERE MATCH(companyName, address, city, state, country, phoneNumber, contact1, sic, faxNumber, keyWords) 
                  AGAINST ('$searchstring') ORDER BY score DESC";
          break;
    
          case "company":
            $sql ="SELECT *, 
                  MATCH(companyName) 
                  AGAINST ('$searchstring') AS score FROM contacts
                  WHERE MATCH(companyName) 
                  AGAINST ('$searchstring') ORDER BY score DESC";
          break;
    
       case "city":
            $sql ="SELECT *, 
                  MATCH(city) 
                  AGAINST ('$searchstring') AS score FROM contacts
                  WHERE MATCH(city) 
                  AGAINST ('$searchstring') ORDER BY score DESC";
          break;
    
       case "state":
            $sql ="SELECT *, 
                  MATCH(state) 
                  AGAINST ('$searchstring') AS score FROM contacts
                  WHERE MATCH(state) 
                  AGAINST ('$searchstring') ORDER BY score DESC";
           break;
    
       case "country":
            $sql ="SELECT *, 
                  MATCH(country) 
                  AGAINST ('$searchstring') AS score FROM contacts
                  WHERE MATCH(country) 
                  AGAINST ('$searchstring') ORDER BY score DESC";
    
           break;
       case "sic":
            $sql ="SELECT *, 
                  MATCH(sic) 
                  AGAINST ('$searchstring') AS score FROM contacts
                  WHERE MATCH(sic) 
                  AGAINST ('$searchstring') ORDER BY score DESC";
    
             break;      
            
           case "keywords":
            $sql ="SELECT *, 
                  MATCH(keyWords) 
                  AGAINST ('$searchstring') AS score FROM contacts
                  WHERE MATCH(keyWords) 
                  AGAINST ('$searchstring') ORDER BY score DESC";
    
             break;      
             } 
        // echo $sql;
        
        $result = mysql_query($sql) or die (mysql_error());
        
        while($row = mysql_fetch_object($result))
        {
          echo '<strong>COMPANY NAME: '.stripslashes(htmlspecialchars($row->companyName)).'</strong><br />';
          echo 'ADDRESS: '.stripslashes(htmlspecialchars($row->address)).'<br />';
          echo 'CITY: '.stripslashes(htmlspecialchars($row->city)).'<br />'; 
          echo 'STATE: '.stripslashes(htmlspecialchars($row->state)).'<br />';
          echo 'COUNTRY: '.stripslashes(htmlspecialchars($row->country)).'<br />';
          echo 'SIC: '.stripslashes(htmlspecialchars($row->sic)).'<br />';
          echo 'PHONE NUMBER: '.stripslashes(htmlspecialchars($row->phoneNumber)).;
          echo 'FAX NUMBER: '.stripslashes(htmlspecialchars($row->faxNumber)).'<br />';     
          echo '<hr size="1" />';
        }
      break;
    }
    ?>
    
    </body>
    </html>
    PHP:

     
    lazarus, Mar 31, 2006 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Just put the form part of your script on your front page and point it to the search script (the action attribute).
     
    exam, Mar 31, 2006 IP
  3. lazarus

    lazarus Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    do i put the search function in the form? cud u please explain because i dont exactly understand how to do it.

    thank you for ur reply..
     
    lazarus, Apr 1, 2006 IP