I am writing a calendar script that returns all known auctions ending on a specific date at a specified distance from a zipcode.
So it should work as follows. User searches by distance from a zipcode, the script returns a calendar with the number of auctions ending displayed on the ending date. That works perfectly!
The problem lies when you increase the distance to greater than 5 miles. The greater the distance supplied the more calendars repeats itself down the page. if you do a distance of 5 miles you get 1 instances of the calendar, 6 you get 2 instances, 10 you get 8 instances and so on and so forth.
So I need an extra set of eyes, can anyone see where it is all going wrong?
[php]<?php
if($theMonth != $monthNames[$cMonth-1]){
echo “<a href=”.$_SERVER[“PHP_SELF”] . “?month=”. $prev_month . “&year=” . $prev_year.">Previous";
}
?>
<?php echo $monthNames[$cMonth-1].' '.$cYear; ?>
<a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>">Next
<div class="daysofweekrow">
<div class="daysofweekcols">
Sunday
</div>
<div class="daysofweekcols">
Monday
</div>
<div class="daysofweekcols">
Tuesday
</div>
<div class="daysofweekcols">
Wednesday
</div>
<div class="daysofweekcols">
Thursday
</div>
<div class="daysofweekcols">
Friday
</div>
<div class="daysofweekcols">
Saturday
</div>
</div>
<?php
$zip = $_GET['zip'];
$distance = $_GET['distance'];
if(preg_match('/^[0-9]{5}$/', $zip)){
if(preg_match('/^[0-9]{1,3}$/', $distance)){
require('includes/db_connect.php');
if(!$rs = mysqli_query($conn, "SELECT * FROM zipcode WHERE postal_code = '$zip'")) {
echo "There was a database error attempting to retrieve your ZIP Code. Please try again.\n";
}
else {
if(mysqli_num_rows($rs) == 0) {
echo "No database match for provided ZIP Code. Please enter a new ZIP Code.\n";
}
else {
//if found, set variables
$row = mysqli_fetch_array($rs);
$lat1 = $row['latitude'];
$lon1 = $row['longitude'];
$d = $distance;
$r = 3959;
//compute max and min latitudes / longitudes for search square
$latN = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(0))));
$latS = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(180))));
$lonE = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(90)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));
$lonW = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(270)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));
$query = "SELECT * FROM zipcode WHERE (latitude <= $latN AND latitude >= $latS AND longitude <= $lonE AND longitude >= $lonW)";
if(!$rs1 = mysqli_query($conn, $query)) {
echo "There was an error selecting nearby ZIP Codes from the database.\n";
}
elseif(mysqli_num_rows($rs1) == 0) {
echo "No nearby ZIP Codes located within the distance specified. Please try a different distance.\n";
}
else {
echo mysqli_error($conn);
//output all matches to screen
while($row = mysqli_fetch_array($rs1)) {
$truedistance = acos(sin(deg2rad($lat1)) * sin(deg2rad($row['latitude'])) + cos(deg2rad($lat1)) * cos(deg2rad($row['latitude'])) * cos(deg2rad($row['longitude']) - deg2rad($lon1))) * $r;
if($truedistance < $d) {
$postal_code = $row['postal_code'];
//build days
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
for ($i=0; $i<($maxday+$startday);$i++) {
if(($i % 7) == 0 ){ echo '