We help IT Professionals succeed at work.
Get Started

How to store multiple values in a PHP array, find the lowest 3 values then display

neilsav
neilsav asked
on
531 Views
Last Modified: 2012-05-10
What I am trying to do:
My website is a public golf course review website - thelocalgolfer.com -
I have a single page for each golf course, on the page I would like to display the closest 5 courses mileage wise on the course display page.  All of the course pages are built dynamically so what happens is I pass a course # to the page, it queries a mysql dbase and grabs all pertinent course information - e.g. course name, latitude, longitude, etc..

I then have another query that pulls the latitude and longitude of a bunch of courses that are near based on region - I then pass each of those latitude and longitude #'s in to a distance function that calculates the mileage distance away from the course that I am displaying on the page.  I store all of the distances in a an array, but I don't know what to do next.  Here is the code so far:

//This function will compare the course and the courses in the same region as that course and return a distance in mileage
function distance($lat1, $lon1, $lat2, $lon2, $unit) { 

  $theta = $lon1 - $lon2; 
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
  $dist = acos($dist); 
  $dist = rad2deg($dist); 
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);

  if ($unit == "K") {
    return ($miles * 1.609344); 
  } else if ($unit == "N") {
      return ($miles * 0.8684);
    } else {
        return $miles;
      }
}

//The array that stores the mileage away from the course that I am displaying
$course_distance= array();
//grabbing all returned records from database comparing the lat and lon vs. the course displayed on the page
while ($z < $nummile) {
//The course id needed for the hyperlink
$id=mysql_result($resultmile,$z,"id");

$course_name=mysql_result($resultmile,$z,"course_name");

$latC=mysql_result($resultmile,$z,"lat");
$lonC=mysql_result($resultmile,$z,"lon");

echo $course_name . " ";
echo distance($lat, $lon, $latC, $lonC, "m") . " miles<br>";
$distanceq=distance($lat, $lon, $latC, $lonC, "m");

$course_distance[] = $distanceq;

  $z++;
					   }

//This will get the course closest, but it doesn't get the three closest and doesn't return coursename or courseid
echo min($course_distance);

Open in new window


I want to take 3 variables- the mileage of the closest course (which comes as an output from the distance function, the course name, and the course id (which I need for the hyperlink).  I then want to display this information.

I am familiar with php, but not so familiar with array's and multidimensional arrays. I do not want to store these values in a dbase, or flatfile, I would like to do all of the calculations on the page itself.  Also, maybe arrays is not the best way, I am open to any other way to accomplish the goal of displaying the 5 closest courses to the subject course.  If you want to see the type of  page that I will display this information check it out here:  http://www.thelocalgolfer.com/course_info_display-course-7.html
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE