Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 18th Jul 2013, 01:22 PM   #1
TNT
 
otfromtot's Avatar
 
Join Date: 2012
Location: florida
Posts: 65
Thanks: 19
Thanked 5 Times in 5 Posts
Default
PHP wildcard search function
Share on: 
fb share twitter share gplus share more share

I have a search form that can search for exact terms and I am trying to make it so you can search a partial term.
If I search for a city, Palm I want it to pull up palm beach, palm beach gardens, and west palm beach. I know that wildcard is % % but I cannot figure out how to add them here

PHP Code:
<?php
error_reporting
(-1);
include(
"config.php");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Zip Code Look Up</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<style>
BODY, TD {
    font-family:Arial, Helvetica, sans-serif;
    font-size:12px;
}
</style>
</head>


<body>

<form id="form1" name="form1" method="post" action="search.php">
<label for="city">City</label>
<input name="city" type="text" id="city" size="10" value="<?php if($_SERVER['REQUEST_METHOD'] === 'POST') { echo $_REQUEST["city"];} ?>" />

<label>State</label>
<select name="state">
<?php

    $sql 
= "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY state ORDER BY state";
    
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);

    while (
$row = mysql_fetch_assoc($sql_result)) {
        if(
$_SERVER['REQUEST_METHOD'] === 'POST') {
        echo 
"<option value='".$row["state"]."'".($row["state"]==$_REQUEST["state"] ? " selected" : "").">".$row["state"]."</option>";
    }
    else {
    echo 
"<option value='".$row["state"]."'>".$row["state"]."</option>";
     } }
?>
</select>
<input type="submit" name="button" id="button" value="Search" />
  </label>
  <a href="search.php"> 
  reset</a>
</form>
<br /><br />
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
    
?>
<table width="700" border="1" cellspacing="0" cellpadding="4">
  <tr>
    <td width="90" bgcolor="#CCCCCC"><strong>Zip</strong></td>
    <td width="95" bgcolor="#CCCCCC"><strong>City</strong></td>
    <td width="159" bgcolor="#CCCCCC"><strong>State</strong></td>
  </tr>
<?php

if ($_REQUEST["city"]<>'') {
    
$search_city = " AND city='".mysql_real_escape_string($_REQUEST["city"])."'";    
}

if (
$_REQUEST["state"]<>'') {
    
$search_state = " AND state='".mysql_real_escape_string($_REQUEST["state"])."'";    
}

if (
$_REQUEST["city"]<>'' and $_REQUEST["state"]<>'') {
    
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE city >= '".mysql_real_escape_string($_REQUEST["city"])."' AND state <= '".mysql_real_escape_string($_REQUEST["state"])."'".$search_city.$search_state;
} else if (
$_REQUEST["city"]<>'') {
    
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE city >= '".mysql_real_escape_string($_REQUEST["city"])."'".$search_city.$search_state;
} else if (
$_REQUEST["state"]<>'') {
    
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE state <= '".mysql_real_escape_string($_REQUEST["state"])."'".$search_city.$search_state;
}

$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (
mysql_num_rows($sql_result)>0) {
    while (
$row = mysql_fetch_assoc($sql_result)) {
?>

<tr>
    <td><?php echo $row["zip"]; ?></td>
    <td><?php echo $row["city"]; ?></td>
    <td><?php echo $row["state"]; ?></td>
  </tr>
  
<?php
    
}
} else {
?>
<tr><td colspan="5">No results found.</td>
<?php    
} }
?>
</table>

</body>
</html>

If everyone knew everything the world sure would be irritating
otfromtot is offline  
Unread 18th Jul 2013, 01:35 PM   #2
VIP Warrior
War Room Member
 
Join Date: 2006
Location: , , USA.
Posts: 1,795
Thanks: 70
Thanked 287 Times in 259 Posts
Default
Re: PHP wildcard search function
Share on: 
fb share twitter share gplus share more share

and city LIKE '%searchterm%'
KirkMcD is offline  
Unread 18th Jul 2013, 01:48 PM   #3
TNT
 
otfromtot's Avatar
 
Join Date: 2012
Location: florida
Posts: 65
Thanks: 19
Thanked 5 Times in 5 Posts
Default
Re: PHP wildcard search function
Share on: 
fb share twitter share gplus share more share

Originally Posted by KirkMcD View Post

and city LIKE '%searchterm%'
PHP Code:
WHERE city LIKE '%".mysql_real_escape_string($_REQUEST["city"])."%'".$search_city.$search_state; 
?

If everyone knew everything the world sure would be irritating
otfromtot is offline  
Unread 19th Jul 2013, 11:25 PM   #4
TNT
 
otfromtot's Avatar
 
Join Date: 2012
Location: florida
Posts: 65
Thanks: 19
Thanked 5 Times in 5 Posts
Default
Re: PHP wildcard search function
Share on: 
fb share twitter share gplus share more share

I've cleaned up the sql calls a bit but am still not able to use the wildcard search

PHP Code:
<?php
error_reporting
(0);
include(
"config.php");
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Zip Code Look Up</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<style>
BODY, TD {
        font-family:Arial, Helvetica, sans-serif;
        font-size:12px;
}
</style>
</head>
 
 
<body>
 
<form id="form1" name="form1" method="post" action="search.php">
<label for="city">City</label>
<input name="city" type="text" id="city" size="10" value="<?php if($_SERVER['REQUEST_METHOD'] === 'POST') { echo $_REQUEST["city"];} ?>" />
 
<label>State</label>
<select name="state">
<?php
 
        $sql 
= "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY state ORDER BY state";
        
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
 
        while (
$row = mysql_fetch_assoc($sql_result)) {
                if(
$_SERVER['REQUEST_METHOD'] === 'POST') {
                echo 
"<option value='".$row["state"]."'".($row["state"]==$_REQUEST["state"] ? " selected" : "").">".$row["state"]."</option>";
        }
        else {
        echo 
"<option value='".$row["state"]."'>".$row["state"]."</option>";
         } }
?>
</select>
<input type="submit" name="button" id="button" value="Search" />
  </label>
  <a href="search.php">
  reset</a>
</form>
<br /><br />
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
        
?>
<table width="700" border="1" cellspacing="0" cellpadding="4">
  <tr>
    <td width="90" bgcolor="#CCCCCC"><strong>Zip</strong></td>
    <td width="95" bgcolor="#CCCCCC"><strong>City</strong></td>
    <td width="159" bgcolor="#CCCCCC"><strong>State</strong></td>
  </tr>
<?php

$sql 
= sprintf("SELECT * FROM %s WHERE city LIKE '%s' AND state LIKE '%s'",
               
$SETTINGS["data_table"],
               
mysql_real_escape_string($_REQUEST['city']),
               
mysql_real_escape_string($_REQUEST['state'])); 
//$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE city LIKE '".mysql_real_escape_string($_REQUEST["city"])."' AND state LIKE '".mysql_real_escape_string($_REQUEST["state"])."'";


$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (
mysql_num_rows($sql_result)>0) {
        while (
$row = mysql_fetch_assoc($sql_result)) {
?>
 
<tr>
    <td><?php echo $row["zip"]; ?></td>
    <td><?php echo $row["city"]; ?></td>
    <td><?php echo $row["state"]; ?></td>
  </tr>
 
<?php
        
}
} else {
?>
<tr><td colspan="5">No results found.</td>
<?php  
} }
?>
</table>
 
</body>
</html>

If everyone knew everything the world sure would be irritating
otfromtot is offline  
Unread 22nd Jul 2013, 07:20 AM   #5
TNT
 
otfromtot's Avatar
 
Join Date: 2012
Location: florida
Posts: 65
Thanks: 19
Thanked 5 Times in 5 Posts
Default
Re: PHP wildcard search function
Share on: 
fb share twitter share gplus share more share

I have tried
PHP Code:
 = sprintf("SELECT * FROM %s WHERE city LIKE '%%s%' AND state LIKE '%s'", 
PHP Code:
 = sprintf("SELECT * FROM %s WHERE city LIKE %%s% AND state LIKE '%s'", 
PHP Code:
 = sprintf("SELECT * FROM %s WHERE city LIKE "%'%s'%" AND state LIKE '%s'", 
PHP Code:
 = sprintf("SELECT * FROM %s WHERE city LIKE '%"%s"%' AND state LIKE '%s'", 
and none of it works. I haven't been able to find a guide that will help get it working with the mysql_real_escape_string()

If everyone knew everything the world sure would be irritating
otfromtot is offline  
Unread 22nd Jul 2013, 09:36 AM   #6
TNT
 
otfromtot's Avatar
 
Join Date: 2012
Location: florida
Posts: 65
Thanks: 19
Thanked 5 Times in 5 Posts
Default
Re: PHP wildcard search function
Share on: 
fb share twitter share gplus share more share

I found it.

PHP Code:
 = sprintf("SELECT * FROM %s WHERE city LIKE '%%%s%%' AND state LIKE '%s'", 
or

PHP Code:
$sql = sprintf("SELECT * FROM %s WHERE city LIKE '%s' AND state LIKE '%s'",
               
$SETTINGS["data_table"],
               
"%".mysql_real_escape_string($_REQUEST['city'])."%",
               
mysql_real_escape_string($_REQUEST['state'])); 

If everyone knew everything the world sure would be irritating
otfromtot is offline  
Closed Thread


Bookmarks

Tags
function, php, search, wildcard


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 06:35 AM.