how to built a search engine

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sheau Wei
    New Member
    • Sep 2006
    • 34

    how to built a search engine

    I am try to using PHP to built a search engine. My problem how to make a search engine that user not only can insert the keyword in text field but also can minimize the searching area by clicking at the option i provide.The most inportant is how to make the relationship between the option user choose and the keyword at test field
  • tbb9216
    New Member
    • Sep 2006
    • 16

    #2
    it all depends on the type of search you want to do. are you having customers search a product, users, or are you trying to take on google?

    say you are searching a product, your SQL would look something like:

    $product = 'web dev book'

    SELECT prodId, prodPrice,
    prodDesc, prodTitle
    FROM products
    WHERE prodTitle LIKE '%$product%'

    this is the most basic kind of search. to break it down, you are selecting the fields you want from the table 'products,' then you are matching $product to prodTitle using LIKE, which allows wildcards (%), so any title that has $product in it anywhere. the downside to this is if $product == 'a', it will return any title with 'a' anywhere in it.

    if you want to have user selected query options to narrow down their selection, you could do:

    $product = 'web dev book'

    if ( $priceSwitch ) {
    $where .= "AND prodPrice = '$prodSwitch'";
    }
    if ( $hardcoverSwitc h ) {
    $where .= "AND prodCover = 'hardcover'";
    }
    SELECT prodId, prodPrice,
    prodDesc, prodTitle
    FROM products
    WHERE prodTitle LIKE '%$product%'
    $where

    this will append your $where to the end of the mysql query, adding the extra switches you want. you could also use OR instead of AND if it fits, and this same concept can be used elsewhere in the script to make it more dynamic.

    if you are daring, you can use REGEX:

    $product = '^web'

    SELECT *
    FROM products
    WHERE prodTitle REGEX '$product'

    this allows regex characters, so in this case it would look for any title that starts with 'web', as ^ is regex for the start of a line. there is some danger in this, as starting with a ? will draw and error, or having a [ without a ], so youll need to parse those. but this should at least get you off to a start.

    let me know if you have more questions.

    -tim

    Comment

    • Sheau Wei
      New Member
      • Sep 2006
      • 34

      #3
      Below is my search engine code. Now i want to add on two radio option so that the searching can be minimize. What should i do?



      <?php session_start() ;?>
      <h2>Search</h2>
      <form name="search" method="post" action="<?=$PHP _SELF?>">
      Search for:
      <input type="text" name="find" /> in
      <Select NAME="field">
      <Option VALUE="Operasi" >Alat Operasi</option>
      <Option VALUE="Balai">A lat Balai</option>
      <Option VALUE="Komunika si">Alat Komunikasi</option>


      </Select>

      <input type="hidden" name="searching " value="yes" />
      <input type="submit" name="search" value="Search" />
      </form>

      <?
      //This is only displayed if they have submitted the form

      if ($searching =="yes")
      {
      echo "<h2>Result s</h2><p>";

      //If they did not enter a search term we give them an error
      if ($find == "")
      {
      echo "<p>You forgot to enter a search term";
      exit;
      }

      // Otherwise we connect to our Database
      mysql_select_db ("inventory" ) or die(mysql_error ());

      // We preform a bit of filtering
      $find = strtoupper($fin d);
      $find = strip_tags($fin d);
      $find = trim ($find);

      //Now we search for our search term, in the field the user specified
      $data = mysql_query("SE LECT * FROM listofitem WHERE upper($field) LIKE'%$find%'") ;


      echo "<table border='1'>";
      echo "<tr> <th>ID</th> <th>NamaPeralat an</th><th>Motor</th><th>Alat Operasi</th><th>Alat Balai</th><th>Alat Komunikasi</th> </tr>";

      //And we display the results
      while($result = mysql_fetch_arr ay( $data ))
      {


      echo "<tr><td>";
      echo $result['ID'];
      echo "</td><td>";
      echo $result['NamaPeralatan'];
      echo "</td><td>";
      echo $result['Motor'];
      echo "</td><td>";
      echo $result['Operasi'];
      echo "</td><td>";
      }
      echo "</table>";



      //This counts the number or results - and if there wasn't any it gives them a little message explaining that
      $anymatches=mys ql_num_rows($da ta);
      if ($anymatches == 0)
      {
      echo "Sorry, but we can not find an entry to match your query<br><br>";
      }

      //And we remind them what they searched for
      echo "<b>Searche d For:</b> " .$find;
      }
      ?>

      Comment

      • tbb9216
        New Member
        • Sep 2006
        • 16

        #4
        2 things i need to know.

        1) what is this a search for, if i understand the language i can help more

        2) what do you want the radio buttons to do?

        let me know this and i can tell you how to do it

        Comment

        • Sheau Wei
          New Member
          • Sep 2006
          • 34

          #5
          Originally posted by tbb9216
          2 things i need to know.

          1) what is this a search for, if i understand the language i can help more

          2) what do you want the radio buttons to do?

          let me know this and i can tell you how to do it
          In the column of Motor it will be catagorize to WithMotor and UnMotor tools.Thus the function of radio is let user can choose either the tools are type of WithMotor or UnMotor.

          When the user key in the keywords in the textfield, the engine will search either the term is from WithMotor or UnMotor, then what categories it come from whether from OperationTool, OfficeTools or ComunicaionTool s


          CREATE TABLE `listofitem` `ID` INT( 10 ) NOT NULL AUTO_INCREMENT ,
          `NameOfTool` VARCHAR( 30 ) NOT NULL ,
          `Motor` VARCHAR( 10 ) NULL DEFAULT NULL ,
          `Operation` VARCHAR( 30 ) NULL DEFAULT NULL ,
          `Office` VARCHAR( 30 ) NULL DEFAULT NULL ,
          `Comunication` VARCHAR( 30 ) NULL DEFAULT NULL

          <?php session_start() ;?>
          <h2>Search</h2>
          <form name="search" method="post" action="<?=$PHP _SELF?>">
          Search for:
          <input type="text" name="find" /> in
          <Select NAME="field">
          <Option VALUE="Operatio n">OperationToo l</option>
          <Option VALUE="Office"> OfficeTools</option>
          <Option VALUE="Comunica tion">Comunicat ionTools</option>


          </Select>

          <input type="hidden" name="searching " value="yes" />
          <input type="submit" name="search" value="Search" />
          </form>

          <?
          //This is only displayed if they have submitted the form

          if ($searching="ye s")

          echo "<h2>Result s</h2><p>";

          //If they did not enter a search term we give them an error
          if ($find == "")
          {
          echo "<p>You forgot to enter a search term";
          exit;
          }

          // Otherwise we connect to our Database
          mysql_select_db ("inventory" ) or die(mysql_error ());

          // We preform a bit of filtering
          $find = strtoupper($fin d);
          $find = strip_tags($fin d);
          $find = trim ($find);

          //Now we search for our search term, in the field the user specified
          $data = mysql_query("SE LECT * FROM listofitem WHERE upper($field) LIKE'%$find%'") ;


          echo "<table border='1'>";
          echo "<tr> <th>ID</th> <th>NameOfTools </th><th>Motor</th><th>Alat Opeation</th><th>OfficeTo ols</th><th>Comunica tionTools</th> </tr>";

          //And we display the results
          while($result = mysql_fetch_arr ay( $data ))
          {


          echo "<tr><td>";
          echo $result['ID'];
          echo "</td><td>";
          echo $result['NameOfTools'];
          echo "</td><td>";
          echo $result['Motor'];
          echo "</td><td>";
          echo $result['Operation'];
          echo "</td><td>";
          echo $result['Comunication'];
          echo "</td><td>";
          }
          echo "</table>";



          //This counts the number or results - and if there wasn't any it gives them a little message explaining that
          $anymatches=mys ql_num_rows($da ta);
          if ($anymatches == 0)
          {
          echo "Sorry, but we can not find an entry to match your query<br><br>";
          }

          //And we remind them what they searched for
          echo "<b>Searche d For:</b> " .$find;

          ?>

          Comment

          Working...