This is my first post so please let me know if I left anything out. Anyways, i'm trying to print a list of the five closest cities to a particular city (the host city). When I run the following code a link with the name Array prints instead of a link with the city name.
The array i'm trying to print from is sorted by it's key which is the distance from the host city to another city. I use a database query and loop to create the array. All of this seems to work fine. But because I'm new to php and programming in general I believe that the problem may be with the syntax i'm using to print the array.
One difficulty I have ran into is printing from an array that has a key that varies. But I have found that using a variable key (distance) is the most accurate way to sort the array to get the closest 5 cities.
Here is what is printing:
Array
Array
Array
Array
Array
Here is an example of what I would like to print:
Seattle
Shoreline
Lynnwood
Federal Way
Tacoma
Code: Select all
<?php
/* Include for the logon information */
include "login.php";
/*=====Sets up the host city=====*/
/* Gets the host cities city and state name */
$hostcity = $_GET['city'];
$hoststate = $_GET['state'];
/*Query the database for the latitude and longitude of the host city*/
$hostquery = "SELECT latitude, longitude FROM cityinfo_cities WHERE city_name='$hostcity' and state_name='$hoststate'";
/*add the query results to the $hostresults variable*/
$hostresult = mysql_query($hostquery)
or die ("Couldn't execute query.");
/*Extracts the latitude and longitude of the host city and assigns the values to variables */
while($row = mysql_fetch_row($hostresult))
{
$hostlatitude = $row[0];
$hostlongitude = $row[1];
}
/*=====Finds the distance between two cities and creates an array=====*/
/*Query the database for latitude and longitude of all cities in the cityinfo_cities table*/
$query = "SELECT city_name, state_name, latitude, longitude FROM cityinfo_cities";
/*Add the query results to the $results variable*/
$result = mysql_query($query)
or die ("Couldn't execute query.");
/*Pulls each cities city name state name latitude and longitude and assigns them to variables. */
while($row = mysql_fetch_array($result))
{
/*$row[] represents a row in the array that is generated by when fetching rows.*/
$city_name = $row[0];
$state_name = $row[1];
$latitude = $row[2];
$longitude = $row[3];
/*Mathmatical formula that computes the distance between two cities and assigns the distance to the $miles variable.*/
$miles = 3963.0 * acos(sin($hostlatitude/57.2958) * sin($latitude/57.2958) + cos($hostlatitude/57.2958) * cos($latitude/57.2958) * cos($longitude/57.2958 - $hostlongitude/57.2958));
/*Sets up an array. $miles (the key) is the distance between the two cities.*/
$cities[$miles] = array ('city' => $city_name, 'state' => $state_name);
}
/*Sort the $cities array by it's key (the distance between two cities)*/
ksort($cities);
/*Prints the first value in the $cities array*/
$value = current ($cities);
if ($value = $hostcity) {
$value = next ($cities);
echo "<a href='http://www.mysite.com/city/{$value}'>{$value}</a><br>";
}
else {
echo "<a href='http://www.mysite.com/city/{$value}'>{$value}</a><br>";
}
/* loops through the next 4 rows in the array and print them out*/
for($i = 0; $i < 4; $i++) {
$value = next ($cities);
echo "<a href='http://www.mysite.com/city/{$value}'>{$value}</a><br>";
}
?>