Ok,
The purpose of this script is to create a Store Location finder: Someone enters their zipcode. If found, a list of stores in that zipcode is printed. If not found, a query is run against the first 3 digits of the zipcode, and then those stores are printed. If the first three didgits don't find a match, then the script prints "sorry" and quits.
I have the first and last condition working fine, it's the middle one we are discussing.
The person uses a form on page 1 to enter their zip, which is passed to page 2, where this happens:
PHP Code:
<?php
$state = addslashes($state);
// CONNECT TO MYSQL
@ $db = mysql_connect("localhost", "blah", "blah");
// CHECK FOR CONNECTION
if (!db)
{
?>
<h2>Could not connect to the database. Please try again later.</h2>
<?
exit;
}
$query = "select * from locations where zip = '$zip' order by '$order_by'";
$result = mysql_db_query("storedb",$query);
$num_results = mysql_num_rows($result);
$query2 = "select * from ingles_loc where zip LIKE '$zip3%' order by zip";
$result2 = mysql_db_query("storedb",$query2);
$num_results2 = mysql_num_rows($result2);
if ($num_results >= 1)
{
echo "
<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
// DISPLAY THE RESULTS OF THE QUERY
$j = 1;
for ($i=0; $i<$num_results; $i++)
{
$row = mysql_fetch_array($result);
// ALTERNATING BACKGROUND COLORS FOR ROWS
echo "<tr valign=\"middle\" align=\"left\" bgcolor=\"";
$bgcolor1 = "#FFFFFF";
$bgcolor2 = "#DDDDDD";
if ( ($j % 2) == 0 ) {
echo $bgcolor1;
} else {
echo $bgcolor2;
}
$j++;
echo "\"><td>";
echo htmlspecialchars( stripslashes($row["store_number"]));
echo "</td><td class=\"tinytext\">";
echo htmlspecialchars( stripslashes($row["address"]));
echo "</td><td class=\"tinytext\">";
echo htmlspecialchars( stripslashes($row["city"]));
echo "</td><td>";
echo htmlspecialchars( stripslashes($row["state"]));
echo "</td><td class=\"tinytext\">";
echo htmlspecialchars( stripslashes($row["zip"]));
echo "</td><td class=\"tinytext\">";
echo htmlspecialchars( stripslashes($row["phone"]));
echo "</td></tr>";
}
}
else {
if ($num_results == 0)
echo "
<p>No stores were found in your zip code. These stores are in zip codes near yours:</p>
<table border=\"0\" cellpadding=\"4\" cellspacing=\"2\">";
// DISPLAY THE RESULTS OF THE QUERY
$j = 1;
for ($i=0; $i<$num_results2; $i++)
{
$row = mysql_fetch_array($result2);
// ALTERNATING BACKGROUND COLORS FOR ROWS
echo "<tr valign=\"middle\" align=\"left\" bgcolor=\"";
$bgcolor1 = "#FFFFFF";
$bgcolor2 = "#DDDDDD";
if ( ($j % 2) == 0 ) {
echo $bgcolor1;
} else {
echo $bgcolor2;
}
$j++;
echo "\"><td>";
echo htmlspecialchars( stripslashes($row["store_number"]));
echo "</td><td class=\"tinytext\">";
echo htmlspecialchars( stripslashes($row["address"]));
echo "</td><td class=\"tinytext\">";
echo htmlspecialchars( stripslashes($row["city"]));
echo "</td><td>";
echo htmlspecialchars( stripslashes($row["state"]));
echo "</td><td class=\"tinytext\">";
echo htmlspecialchars( stripslashes($row["zip"]));
echo "</td><td class=\"tinytext\">";
echo htmlspecialchars( stripslashes($row["phone"]));
echo "</td></tr>";
}
if ($num_results2 == 0)
echo "<p>The zip code you entered does not match any records in our database.</p>";
}
?>
As mentioned before, I'm still fumbling my way through this, so it's likely there is a better way to do it.
The zip field in the database is int(5).
Hope that helps to convey what's going on. Let me know if you need any more info. Again, thanks for your time.
Bookmarks