Search Function with Multi Terms

Discussion in 'PHP' started by a4tech, Apr 15, 2013.

  1. #1
    Hello, This is my table. I am wanted to create a search function.

    CREATE TABLE phone_record (
        id INT(10) NOT NULL AUTO_INCREMENT,
        phoneno VARCHAR(255) NOT NULL,
        PRIMARY KEY (id),
        UNIQUE KEY (phoneno),
    );
    Code (markup):
    This is my php code. Here I want 2 things.

    First is this is for one phone no. search at a time like (000.000.000). How to change this that it can handle the multi phone no same time and gives the output for both.

    Second If I do exact search like SELECT * FROM phone_record WHERE (phoneno = '%".$_GET["txtKeyword"]."%') it did not work is this because of dot (.). Is there any solution for this.

    <form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
      <table width="599" border="1">
        <tr>
          <th>Keyword
          <input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>">
          <input type="submit" value="Search"></th>
        </tr>
      </table>
    </form>
    <?
    if($_GET["txtKeyword"] != "")
        {
        $objConnect = mysql_connect("localhost") or die("Error Connect to Database");
        $objDB = mysql_select_db("phone_record");
        // Search By Phone No.
        $strSQL = "SELECT * FROM phone_record WHERE (phoneno LIKE '%".$_GET["txtKeyword"]."%')";
        $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
        ?>
        <table width="600" border="1">
          <tr>
              <th width="91"> <div align="center">ID </div></th>
            <th width="91"> <div align="center">Phone No:</div></th>
            </tr>
        <?
        while($objResult = mysql_fetch_array($objQuery))
        {
        ?>
          <tr>
              <td><div align="center"><?=$objResult["id"];?></div></td>
            <td><div align="center"><?=$objResult["phoneno"];?></div></td>
          </tr>
        <?
        }
        ?>
        </table>
    PHP:
    Thank You.
     
    a4tech, Apr 15, 2013 IP
  2. ChiragKalani

    ChiragKalani Active Member

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    73
    #2
    Hi, I would suggest, Store phone number without .(dot)

    This may help you in solving both the problem.
     
    ChiragKalani, Apr 15, 2013 IP
  3. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #3
    I'm not sure what you mean in your first question. As for your second question, you could try using something like MySQL's Replace function in your WHERE claus to temporarily get rid of that fullstop. Otherwise, like mentioned previously, you could simply store the phone number without the fullstops, and format it accordingly when you want to output it instead.
     
    Alex Roxon, Apr 15, 2013 IP
  4. dizyn

    dizyn Active Member

    Messages:
    251
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Either you save phone numbers without "." or you just use "Replace" function to remove "." from phone number.
     
    dizyn, Apr 16, 2013 IP