PDA

View Full Version : PHP/MySQL Help Please...


misteraven
08-07-2002, 11:23 AM
I'm still kind of new to PHP and have been stuck on a particular issue. I'm trying to develope a store locator that's basically a MySQL database of addresses that'll allow a person to search for a store address by either city, state or country. I've got most of it worked out, but was trying to figure out how I can limit the results shown to a certain number that would be predefined in variable. Further, if the results exceeded another number (also predefined ina variable), it would produce an error page where I could put in a message to narrow the search parameters. Any help or a link that might get me there would be very greatly appreciated. Here's the Search/Results Page so far...



<?php

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

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


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

echo "<table>";
echo "<tr><td>$resultsnumber results found<br><br></td></tr>";

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

if ($alternate == "1") {
$color = "#ffffff";
$alternate = "2";
}
else {
$color = "#efefef";
$alternate = "1";
}
echo "<tr><td>$storename</td>";
echo "<td><a href='view.php?id=$id'>see details</a></td></tr>";
}
echo "<tr><td><br><br>Look <a href='az_index.php'>alphabetically</a> or <a href='$PHP_SELF'>search again</a></td></tr>";
echo "</table>";

}


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

<table border="0" cellspacing="2" width="380">
<tr>
<td><b>Search</b></td>
<td><b>Variable</b></td>
</tr>
<tr>
<td valign="top">
<input type="text" name="searchstring" size="35">
</td>
<td>
<input type="radio" name="searchtype" value="storename">Storename<br>
<input type="radio" name="searchtype" value="city">City<br>
<input type="radio" name="searchtype" value="state" checked>State<br>
<input type="radio" name="searchtype" value="zip">Zip<br>
<input type="radio" name="searchtype" value="country">Country</td>
</tr>
<tr><td><input type="submit" value="Submit"></td>
<td>[nbsp]</td></tr>

</table>
</form>
<?
}

include ("include/footer.inc.php");
?>

Matt
08-07-2002, 11:55 AM
You need to use LIMIT to limit your results to a certain number:
$sql="SELECT * FROM $table WHERE $searchtype LIKE '%$searchstring%' ORDER BY storename ASC LIMIT $max";

To see if your results exceed a certain number, you would need to use COUNT:
$sql="SELECT COUNT(*) FROM $table WHERE $searchtype LIKE '%$searchstring%' ORDER BY storename ASC";

Hope this helps- Matt

misteraven
08-07-2002, 11:57 AM
Matt,

Thanks for the help. I'll give it a shot.

misteraven
08-07-2002, 01:06 PM
Alright, the LIMIT command was simple enough (never had to use it before), but I'm not sure I understand how to do the second part. I think to have the second part be a seperate statement would be redundant so rather, how would I add to LIMIT so that if it did exceed this LIMIT number, some error text would appear above the results?

Here's the modified code:




<?php

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

$max = "3";

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


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

echo "<table>";
echo "<tr><td>$resultsnumber results found<br><br></td></tr>";

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

if ($alternate == "1") {
$color = "#ffffff";
$alternate = "2";
}
else {
$color = "#efefef";
$alternate = "1";
}
echo "<tr><td>$storename</td>";
echo "<td><a href='view.php?id=$id'>see details</a></td></tr>";
}
echo "<tr><td><br><br><a href='$PHP_SELF'>search again</a></td></tr>";
echo "</table>";

}


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

<table border="0" cellspacing="2" width="380">
<tr>
<td><b>Search</b></td>
</tr>
<tr>
<td><input type="hidden" name="searchtype" value="state">
<select name="searchstring">
<option value="" selected>Select</option>
<option value="AK">ALASKA</option>
<option value="AL">ALABAMA</option>
<option value="AR">ARKANSAS</option>
<option value="AZ">ARIZONA</option>
<option value="CA">CALIFORNIA</option>
<option value="CO">COLORADO</option>
<option value="CT">CONNECTICUT</option>
<option value="DC">DISTRICT OF COLUMBIA</option>
<option value="DE">DELAWARE</option>
<option value="FL">FLORIDA</option>
<option value="GA">GEORGIA</option>
<option value="HI">HAWAII</option>
<option value="IA">IOWA</option>
<option value="ID">IDAHO</option>
<option value="IL">ILLINOIS</option>
<option value="IN">INDIANA</option>
<option value="KS">KANSAS</option>
<option value="KY">KENTUCKY</option>
<option value="LA">LOUISIANA</option>
<option value="MA">MASSACHUSETTS</option>
<option value="MD">MARYLAND</option>
<option value="ME">MAINE</option>
<option value="MI">MICHIGAN</option>
<option value="MN">MINNESOTA</option>
<option value="MO">MISSOURI</option>
<option value="MS">MISSISSIPPI</option>
<option value="MT">MONTANA</option>
<option value="NC">NORTH CAROLINA</option>
<option value="ND">NORTH DAKOTA</option>
<option value="NE">NEBRASKA</option>
<option value="NH">NEW HAMPSHIRE</option>
<option value="NJ">NEW JERSEY</option>
<option value="NM">NEW MEXICO</option>
<option value="NV">NEVADA</option>
<option value="NY">NEW YORK</option>
<option value="OH">OHIO</option>
<option value="OK">OKLAHOMA</option>
<option value="OR">OREGON</option>
<option value="PA">PENNSYLVANIA</option>
<option value="RI">RHODE ISLAND</option>
<option value="SC">SOUTH CAROLINA</option>
<option value="SD">SOUTH DAKOTA</option>
<option value="TN">TENNESSEE</option>
<option value="TX">TEXAS</option>
<option value="UT">UTAH</option>
<option value="VA">VIRGINIA</option>
<option value="VT">VERMONT</option>
<option value="WA">WASHINGTON</option>
<option value="WI">WISCONSIN</option>
<option value="WV">WEST VIRGINIA</option>
<option value="WY">WYOMING</option>

</select>

</td>
</tr>

<tr><td><input type="submit" value="Submit"></td>
<td>[nbsp]</td></tr>

</table>
</form>
<?
}

include ("include/footer.inc.php");
?>

Matt
08-08-2002, 01:32 AM
Since no one else has responded, I'll make a few additional remarks. First of all, the SELECT COUNT(*) command [i]is[i/] a bit redundant. For the record, you should simplify the processing necessary by shortening it to:
$sql="SELECT COUNT(*) FROM $table WHERE $searchtype LIKE '%$searchstring%'";

The idea is to first count how many records you have. If it exceeds the threshold you've set, then you would never need to proceed with the actual command to pull all matching entries from the database.

There are MANY ways to simplify the logic further I'm sure, although I don't know how easy it would be to get MySQL to return an error code if you exceed some record limit. How many records would the table you're searching contain? You should definitely consider buying Dubois' book MySQL published by New Riders available at amazon for ~$35 if you are serious about MySQL.