Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > SQL Search Query: How To?

SQL Search Query: How To?
Thread Tools
Mac Enthusiast
Join Date: Jul 2003
Status: Offline
Reply With Quote
Apr 28, 2006, 12:19 AM
 
I have been modifying a php script that uses a mysql database to build an address book. The script comes with a search function that allows you to search by first name or last name. I created a new variable called $address_state_home and made a table entry for this. Now, i am trying to allow the user to also search the database by state. Can any PHP gurus out there help me figure out how to incorporate the ability to search by state?

Here is the existing code:

<?php

include ("include/header.inc.php");
include ("include/dbconnect.php");

if ($searchstring)
{
$sql="SELECT * FROM $table WHERE $searchtype LIKE '%$searchstring%' ORDER BY lastname ASC";


$result = mysql_query($sql);
$resultsnumber = mysql_numrows($result);

echo "<TABLE BORDER=0>";
echo "number of results: $resultsnumber";

$alternate = "2";
while ($myrow = mysql_fetch_array($result))
{
$firstname = $myrow["firstname"];
$id = $myrow["id"];
$lastname = $myrow["lastname"];

if ($alternate == "1") {
$color = "#ffffff";
$alternate = "2";
}
else {
$color = "#efefef";
$alternate = "1";
}
echo "<TR bgcolor=$color><TD>$lastname, $firstname</TD>";
echo "<td><a href='view.php?id=$id'>see details</a></td><td><a href='edit.php?id=$id'>edit record</a></td><td><a href='delete.php?id=$id' onClick=\"return confirm('Are you sure?')\">delete record</a></td>";
}
echo "</TR></TABLE>";
echo "<p>Look <a href='az_index.php'>alphabetically</a> or <a href='$PHP_SELF'>search again</a></p>";

}


else
{
?>
<form method="POST" action="<? $PHP_SELF ?>">

<table border="0" cellspacing="2" width="380">
<tr>
<td><h1>Search for</h1></td>
<td><b>Part of address book to search</b></td>
</tr>
<tr>
<td valign="top">
<input type="text" name="searchstring" size="35">
</td>
<td>
<input type="radio" name="searchtype" value="lastname">Lastname<br>
<input type="radio" name="searchtype" value="firstname" checked>Firstname<br>
<input type="radio" name="searchtype" value="address">State<br>
<input type="radio" name="searchtype" value="email">Email</td>
</tr>
<tr><td><input type="submit" value="Submit"></td>
<td>&nbsp;</td></tr>

</table>
</form>
<form method="POST" action="<? $PHP_SELF ?>">

<table border="0" cellspacing="2" width="380">
<tr>
<td>Or show all people with birthdays in selected month:<b></b></td>
</tr>
<tr>
<td valign="top">
<input type="hidden" name="searchtype" value="bmonth">
<select name="searchstring">
<option value="January" selected>January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="show">
</td>
</tr>
</table>
</form>
<?
}

#include ("include/footer.inc.php");
?>
     
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Apr 28, 2006, 01:20 PM
 
We need the header and footer code too.
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
Mac Enthusiast
Join Date: Jul 2003
Status: Offline
Reply With Quote
Apr 28, 2006, 04:25 PM
 
All that's in there is Things like style sheet and non-essential stuff- I can post it if it helps.

Thanks
     
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status: Offline
Reply With Quote
Apr 28, 2006, 05:18 PM
 
$sql="SELECT * FROM $table WHERE $searchtype LIKE '%$searchstring%' ORDER BY lastname ASC";

translates to :

SELECT everything FROM [the table named $table] WHERE [the search type is $searchtype -- probably something like 'last_name'] matches [wildcard (% is the wildcard character) + [whatever the $searchstring is] + wildcard] and then ORDER the results by lastname in ASCending order.

You'll need to fill in the variables with applicable data.
     
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Apr 29, 2006, 02:00 AM
 
I'd like to look at those two includes up top, to see how the original writer defined the variables.
selecting the "State" radio button should set $searchtype to 'address' which would be a valid column in the table, and it should search just fine. Did you make that change, or was that like that?
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
Mac Enthusiast
Join Date: Jul 2003
Status: Offline
Reply With Quote
Apr 29, 2006, 09:04 AM
 
I tinkered little by little and got it to work. I don't want to clog up the airwaves with it, but I'd be happy to post the end result. I'd also be happy to share the entire script- it's a useful little online address utility. I was able to customize it to my needs with minimal php/mysql knowledge (I know enough to be dangerous). The issue was somewhere towards the bottom of the script.

Thanks again for taking the time- again, let me know, I'd be happy to share it all.
     
Mac Elite
Join Date: Oct 2000
Location: Seattle
Status: Offline
Reply With Quote
May 1, 2006, 04:48 AM
 
Do you have the whole address in a single address field?
When searching and sorting it is way more useful to have separate fields for all the pieces. This also will make it easier to do things like export your data as a vcard, etc.

street_address
street_address_2 (many addrs have a second or even third line)
city
state
zip
country

My searches usually allow for any of the fields to be searched on all at the same time, and I allow the user the decide the sort field and direction.
The more fields you fill in the more it restrics the found set by adding AND clauses to the query. So you can get all the Smiths in Texas with a D in their first name ordered by zip code.

$sql = "SELECT *
FROM $table
WHERE id > 0 "; // default to get everything here because we might not have anything below.

if($name_first) $sql .= " AND name_first LIKE '%$name_first%' ";
if($name_last) $sql .= " AND name_last LIKE '%$name_last%' ";
if($email) $sql .= " AND email LIKE '%$email%' ";
if($street_address) $sql .= " AND street_address LIKE '%$street_address%' ";
if($street_address_2) $sql .= " AND street_address_2 LIKE '%$street_address_2%' ";
if($city) $sql .= " AND city LIKE '%$city%' ";
if($state) $sql .= " AND state = '$state' ";
if($zip) $sql .= " AND zip = '$zip' ";

$sql .= "ORDER BY $sort_field $sort_direction";

You can reuse the edit form as the search form.

Also, I usually put the search string back into the search box so they can easily change the search without retyping everything.

<input type="text" name="searchstring" size="35" value="<? echo $searchstring; ?>">

have fun
You can take the dude out of So Cal, but you can't take the dude outta the dude, dude!
     
   
Thread Tools
Forum Links
Forum 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 On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 10:47 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2