[nycphp-talk] Help with error statement
Rob Marscher
rmarscher at beaffinitive.com
Thu Nov 16 14:58:08 EST 2006
Gotcha. Try this:
$zips = $z->get_zips_in_range('07732', '10',
_ZIPS_SORT_BY_DISTANCE_ASC, true);
$row_count++;
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 'Sorry, your search returned no locations. Try using a larger
radius to search.';
}
Paul wrote:
> The problem with this is that the count is always greater than zero.
> The count($key) will always equal at least one. The one being the
> orignating zipcode that you searched since even if you searched 1
> mile, you would still have one originating zipcode. If there is a
> location at that originating zipcode it would also return a location
> query. Hence my problem.
>
> Paul Guba
>
>
> On Nov 16, 2006, at 12:56 PM, Rob Marscher wrote:
>
>
>> Sorry... I looked at it a little too quickly... You need to put
>> your if
>> statement outside the foreach. That was the issue.
>>
>> $zips = $z->get_zips_in_range('07732', '10',
>> _ZIPS_SORT_BY_DISTANCE_ASC, true);
>> if(isset($zips) && count($key) > 0)
>> //find all locations within range using returned zipcode values
>> {
>> foreach ($zips as $key => $value)
>> {
>> $sql_events = mysql_query("SELECT * FROM area36 WHERE zipcode
>> ='$key'") or die (mysql_error());
>> while ($row = mysql_fetch_array($sql_events)) {
>> // process rows
>> }
>> }
>> }
>> // error statement if no locations are found within search radius
>> else {
>> echo 'Sorry, your search returned no locations. Try using a larger
>> radius to search.';
>> }
>>
>>
>>
>> Paul wrote:
>>
>>> If I do as I think you suggested here:
>>> On Nov 16, 2006, at 11:35 AM, Rob Marscher wrote:
>>>
>>>
>>>
>>>> You want to use mysql_num_rows -
>>>> http://us3.php.net/manual/en/function.mysql-num-rows.php - after
>>>> your
>>>> query. You can then do an if statement to check if mysql_num_rows !
>>>> = 0
>>>> before doing the while loop:
>>>>
>>>> $sql_events = mysql_query("SELECT * FROM area36 WHERE zipcode
>>>> ='$key'") or die (mysql_error());
>>>> if (mysql_num_rows() !== 0)
>>>> {
>>>> while ($row = mysql_fetch_array($sql_events)) {
>>>> // stuff in here
>>>> }
>>>> }
>>>> else
>>>> {
>>>> // error - no records found
>>>> }
>>>>
>>>>
>>> My correction snippet based on that here:
>>>
>>> {
>>> $sql_events = mysql_query("SELECT * FROM area36 WHERE zipcode
>>> ='$key'") or die (mysql_error());
>>> if (mysql_num_rows($sql_events) !== 0)
>>> {
>>> 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++;
>>> }
>>> }
>>> // error statement if no locations are found within search radius
>>> else {
>>> echo 'Sorry, your search returned no locations. Try using a larger
>>> radius to search.';
>>> }
>>>
>>>
>>>
>>> Returns the error statement each time there is an x amount positive
>>> result. This was the exact problem I was having. So what I get is
>>> the error repeated x amount of times than my table is built with the
>>> actual results. What am I missing?
>>>
>>> Paul Guba
>>>
>>>
>>> _______________________________________________
>>> New York PHP Community Talk Mailing List
>>> http://lists.nyphp.org/mailman/listinfo/talk
>>>
>>> NYPHPCon 2006 Presentations Online
>>> http://www.nyphpcon.com
>>>
>>> Show Your Participation in New York PHP
>>> http://www.nyphp.org/show_participation.php
>>>
>>>
>>>
>> --
>> Rob Marscher
>> Software Engineer
>> rmarscher at beaffinitive.com
>> 212.684.9100x17
>>
>> _______________________________________________
>> New York PHP Community Talk Mailing List
>> http://lists.nyphp.org/mailman/listinfo/talk
>>
>> NYPHPCon 2006 Presentations Online
>> http://www.nyphpcon.com
>>
>> Show Your Participation in New York PHP
>> http://www.nyphp.org/show_participation.php
>>
>
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
>
>
--
Rob Marscher
Software Engineer
rmarscher at beaffinitive.com
212.684.9100x17
More information about the talk
mailing list