...

View Full Version : Need to add error statement



gubaguba
11-14-2006, 02:03 PM
I have the beginings of a zipcode dealer locator. Currently it has actual zip and milage numbers but I will replace these with variables latter. What I would like for it to do is to return an error message if no meetings are found. Originally I thought to use zip count as shown but realised a value of one is always returned for the zip entered. There could actually be a one location associated with that zip as well. So I am open to suggestions. Here is what I have so far.

<?php
require_once('zipcode.class.php');
mysql_connect("localhost", "something", "something") or die(mysql_error());
mysql_select_db("ziplocator") or die(mysql_error());
$z = new zipcode_class;
echo "<table>";
echo "<center><tr> <th>Dist</th><th>Day</th> <th>Town</th><th>Time</th><th>Place</th><th>Street</th><th>Type</th><th>Format</th><th>HCA</th><th>NS</th> </tr>";

// Define your colors for the alternating rows

$color1 = "meet";
$color2 = "altmeet";
$row_count = 0;

$zips = $z->get_zips_in_range('07732', '10', _ZIPS_SORT_BY_DISTANCE_ASC, true);
foreach ($zips as $key => $value){
if(isset($key) && count($key) > 0) {
$sql_events = mysql_query("SELECT * FROM area36 WHERE zipcode ='$key'") or die (mysql_error());
while ($row = mysql_fetch_array($sql_events)) {
$dist = $row['dist'];
$day = $row['day'];
$town = $row['town'];
$time = $row['time'];
$place = $row['place'];
$street = $row['street'];
$type = $row['type'];
$Special = $row['Special'];
$hca = $row['hca'];
$ns = $row['ns'];

/* Now we do this small line which is basically going to tell
PHP to alternate the colors between the two colors we defined above. */

$row_color = ($row_count % 2) ? $color1 : $color2;

// Echo your table row and table data that you want to be looped over and over here.

echo "<tr>
<td class='$row_color'>$dist</td>
<td class='$row_color'>$day</td>
<td class='$row_color'>$town</a></td>
<td class='$row_color'>$time</a></td>
<td class='$row_color'>$place</a></td>
<td class='$row_color'>$street</a></td>
<td class='$row_color'>$type</a></td>
<td class='$row_color'>$Special</a></td>
<td class='$row_color'>$hca</a></td>
<td class='$row_color'>$ns</a></td>
</tr>";

// Add 1 to the row count

$row_count++;
}
}
else {
echo 'Sorry, your search returned no results for dealers in your location';
}
}
// Close out your table.

echo "</table>";
?>



Thanks

Paul Guba

Brandoe85
11-14-2006, 02:15 PM
You can check how many rows are returned with mysql_num_rows:
http://us2.php.net/mysql_num_rows

good luck;

gubaguba
11-14-2006, 02:25 PM
You can check how many rows are returned with mysql_num_rows:
http://us2.php.net/mysql_num_rows

good luck;

First off Thanks. Since the first query will always return at least one row are you suggesting to use the mysql_num_row: on the second query that looks up locations?

Paul Guba

gubaguba
11-14-2006, 05:05 PM
Basically I am running into the same problem as before using mysql_num_rows. There will always be at least one row of data returned for the search that of the zipcode entered. Having tried


$zips = $z->get_zips_in_range('07732', '10', _ZIPS_SORT_BY_DISTANCE_ASC, true);
foreach ($zips as $key => $value){
$sql_events = mysql_query("SELECT * FROM area36 WHERE zipcode ='$key'") or die (mysql_error());
$num_rows = mysql_num_rows($sql_events);
if ($num_rows < 1) {
echo 'Sorry, your search returned no results for dealers in your location';
}
else {
while ($row = mysql_fetch_array($sql_events)) {
$dist = $row['dist'];
$day = $row['day'];
$town = $row['town'];
$time = $row['time'];
$place = $row['place'];
$street = $row['street'];
$type = $row['type'];
$Special = $row['Special'];
$hca = $row['hca'];
$ns = $row['ns'];

/* Now we do this small line which is basically going to tell
PHP to alternate the colors between the two colors we defined above. */

$row_color = ($row_count % 2) ? $color1 : $color2;

// Echo your table row and table data that you want to be looped over and over here.

echo "<tr>
<td class='$row_color'>$dist</td>
<td class='$row_color'>$day</td>
<td class='$row_color'>$town</a></td>
<td class='$row_color'>$time</a></td>
<td class='$row_color'>$place</a></td>
<td class='$row_color'>$street</a></td>
<td class='$row_color'>$type</a></td>
<td class='$row_color'>$Special</a></td>
<td class='$row_color'>$hca</a></td>
<td class='$row_color'>$ns</a></td>
</tr>";

// Add 1 to the row count

$row_count++;
}
}
}

// Close out your table.

echo "</table>";
?>

It repeats the error message for each row retuned. Basically the same problem I had before.

Brandoe85
11-14-2006, 06:53 PM
I only see one query?

Or is this the line that returns the rows?
$zips = $z->get_zips_in_range('07732', '10', _ZIPS_SORT_BY_DISTANCE_ASC, true);

I guess i'm confused on which one you need to check...do you need to check that the array returned from that statement has values or the query inside it?

gubaguba
11-14-2006, 07:32 PM
I only see one query?

Or is this the line that returns the rows?
$zips = $z->get_zips_in_range('07732', '10', _ZIPS_SORT_BY_DISTANCE_ASC, true);

I guess i'm confused on which one you need to check...do you need to check that the array returned from that statement has values or the query inside it?




Yes that statement uses the include which makes the calculations for zipcode in the the radius of 10 miles it was written by Micah Carrick. It is available for download freely at several locations. It returns all the zipcodes which are the variable $key. This statement

$sql_events = mysql_query("SELECT * FROM area36 WHERE zipcode ='$key'") or die (mysql_error());

than querys the table looking for all locations with zipcodes matching $key.

Since the $key will always have at least one entry (the originating zipcode and a location maybe at that zipcode) I have not been able to figure out how to use it for the error. My thought would be to use the:
$sql_events = mysql_query("SELECT * FROM area36 WHERE zipcode ='$key'") or die (mysql_error());
for error checking if it returned no matching results. That is where I am having trouble.

gubaguba
11-17-2006, 01:22 PM
I posted on another site http://www.nyphp.org one of the members there was able to provide a solution. It is a great resource of knowledge for php and you don't need to be from NYC to participate. Here is the solution.


<?php
require_once('zipcode.class.php');
mysql_connect("localhost", "something", "something") or die
(mysql_error());
mysql_select_db("ziplocator") or die(mysql_error());
echo '<div align="center"><table>';
echo "<tr> <th>Dist</th><th>Day</th> <th>Town</th><th>Time</th><th>Place</th><th>Street</th><th>Type</th><th>Format</th><th>HCA</th><th>NS</th> </tr>";

// colors of returned rows uses style sheet

$color1 = "meet";
$color2 = "altmeet";
$row_count = 0;
// find all zipcodes values within zip , mile radius use zipcode.class.php written by: Micah Carrick
$z = new zipcode_class;
$zips = $z->get_zips_in_range($_POST['zip_code'], $_POST['miles'], _ZIPS_SORT_BY_DISTANCE_ASC, true);
// add searched for zip to $zips array
$zips[$_POST['zip_code']] = 0;
$row_count = 0;

foreach ($zips as $key => $value)
{
//find all locations within range using returned zipcode values
$sql_events = mysql_query("SELECT * FROM area36 WHERE zipcode
='$key'") or die (mysql_error());
while ($row = mysql_fetch_array($sql_events)) {
$dist = $row['dist'];
$day = $row['day'];
$town = $row['town'];
$time = $row['time'];
$place = $row['place'];
$street = $row['street'];
$type = $row['type'];
$Special = $row['Special'];
$hca = $row['hca'];
$ns = $row['ns'];

//row count for alternating colors
$row_color = ($row_count % 2) ? $color1 : $color2;

//create tables for data row loop
echo "<tr>
<td class='$row_color'>$dist</td>
<td class='$row_color'>$day</td>
<td class='$row_color'>$town</a></td>
<td class='$row_color'>$time</a></td>
<td class='$row_color'>$place</a></td>
<td class='$row_color'>$street</a></td>
<td class='$row_color'>$type</a></td>
<td class='$row_color'>$Special</a></td>
<td class='$row_color'>$hca</a></td>
<td class='$row_color'>$ns</a></td>
</tr>";

// Add 1 to the row count
$row_count++;
}
}
if ($row_count == 0)
// error statement if no locations are found within search radius
{
echo "<tr><td colspan='10' class='$color1'>Sorry, your search returned no locations. Try using a larger
radius to search.</td></tr>";
}
// end table

echo "</table></div>";
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum