Results 1 to 3 of 3

Thread: patterning matching issues

  1. #1
    Join Date
    Sep 2008
    Posts
    12
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default patterning matching issues

    Hey Everyone,

    Having some trouble with ereg function in my PHP script. The user should be able to query the db either by the ID number or surname, however I want to apply some patterning matching to prevent a user from querying a single number or digit and have a large number of entries returned when searching by ID.

    The ID appears in four ways, "ABCD123" "ABCD12" and "AB123" "AB12" now I want a user to be able to query using any of the those four examples, however my script doesnt appear to be working, any help would be great.

    Code:
    <body>
    
    
    <form name="search.php" method="post" action="<?=$PHP_SELF?>">
    Seach for: <input type="text" name="find" /> in
    <Select NAME="field">
    <Option VALUE="id">ID Number</option>
    <Option VALUE="surname">Surname</option>
    </Select>
    <input type="hidden" name="searching" value="yes" />
    <input type="submit" name="search" value="Search" />
    </form>
    
    <?
    
    if ($searching =="yes")
    {
    echo "<h2>Results</h2><p>";
    
    
    if ($find == "")
    {
    echo "<p>You forgot to enter a search term";
    exit;
    }
    
    
    mysql_connect('localhost', 'user', 'pword') or die(mysql_error());
    mysql_select_db("server") or die(mysql_error());
    
    
    function checkfind($find) {
    $find = strtoupper(str_replace(chr(32),'',$find));
    if (ereg ("^(AB)([0-9])|^(ABCD)([0-9])",$find));
    
    return $find;
    else
    return FALSE;
    
    }
    
    
    $find = strip_tags($find);
    $find = trim ($find);
    $find = str_replace(' ','',$find);
    
    
    $data = mysql_query("SELECT * FROM officers WHERE upper($field) LIKE'%$find%'");
    
    
    while($result = mysql_fetch_array( $data ))
    {
    echo $result['id'];
    echo " ";
    echo $result['surname'];
    echo "<br>";
    echo $result['url'];
    echo "<br>";
    echo $result['expiry'];
    echo " ";
    echo "<br>";
    }
    
    
    $anymatches=mysql_num_rows($data);
    if ($anymatches == 0)
    {
    echo "Sorry, but we can not find an entry to match your query<br><br>";
    }
    
    
    echo "<b>Searched For:</b> " .$find;
    }
    ?> 
    
    </body>
    </html>

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    try:
    PHP Code:
    ereg ("^(AB[0-9]{2,3}|ABCD[0-9]{2,3})",$find
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  3. The Following User Says Thank You to Master_script_maker For This Useful Post:

    SirTom909 (04-16-2009)

  4. #3
    Join Date
    Sep 2008
    Posts
    12
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    thank you very much for your help, I incorporated your ereg into my code however im still getting a PHP error, grateful if you could take a look. The rest of the code has been tested and works fine.

    Code:
    <body>
    
    
    <form name="search.php" method="post" action="<?=$PHP_SELF?>">
    Seach for: <input type="text" name="find" /> in
    <Select NAME="field">
    <Option VALUE="id">ID Number</option>
    <Option VALUE="surname">Surname</option>
    </Select>
    <input type="hidden" name="searching" value="yes" />
    <input type="submit" name="search" value="Search" />
    </form>
    
    <?
    
    if ($searching =="yes")
    {
    echo "<h2>Results</h2><p>";
    
    
    if ($find == "")
    {
    echo "<p>You forgot to enter a search term";
    exit;
    }
    
    
    mysql_connect('localhost', 'un', 'pw') or die(mysql_error());
    mysql_select_db("server") or die(mysql_error());
    
    
    function checkfind($find) {
    $find = strtoupper(str_replace(chr(32),'',$find));
    if ereg ("^(ABCD[0-9]{2,3}|AB[0-9]{2,3})",$find);
    
    return $find;
    else
    return FALSE;
    
    }
    
    
    $find = strip_tags($find);
    $find = trim ($find);
    $find = str_replace(' ','',$find);
    
    
    $data = mysql_query("SELECT * FROM officers WHERE upper($field) LIKE'%$find%'");
    
    
    while($result = mysql_fetch_array( $data ))
    {
    echo $result['id'];
    echo " ";
    echo $result['surname'];
    echo "<br>";
    echo $result['url'];
    echo "<br>";
    echo $result['expiry'];
    echo " ";
    echo "<br>";
    }
    
    
    $anymatches=mysql_num_rows($data);
    if ($anymatches == 0)
    {
    echo "Sorry, but we can not find an entry to match your query<br><br>";
    }
    
    
    echo "<b>Searched For:</b> " .$find;
    }
    ?> 
    
    </body>

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
  •