Page 1 of 1

Unable to print from a multidimensional array

Posted: Tue Jul 22, 2008 1:36 am
by interweave34
Hi Everyone,

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>";
    }
?>
 
 

Re: Unable to print from a multidimensional array

Posted: Tue Jul 22, 2008 2:04 am
by Stryks
The output you are getting is due tho the fact that $value is an array.

You need to specify an array element to output. eg.

Code: Select all

echo $value['city'];
I'd also consider just using a foreach or even a while loop instead of splitting the loop into 'first element' and 'the rest' loops.

Cheers

Re: Unable to print from a multidimensional array

Posted: Tue Jul 22, 2008 3:17 am
by interweave34
That worked. Thanks Stryks. In regards to your comment about using the foreach or while loop, can I use foreach to display only 5 cities out of an array with around 1000 cities? When I tried using a foreach loop it brought back all the entries in the array. I'm sure I just have to learn more about how to use a foreach loop. Thanks again. I appreciate the help.

Re: Unable to print from a multidimensional array

Posted: Tue Jul 22, 2008 7:19 am
by Stryks
I'd probably go with foreach, with a simple counter to break out of the loop when the correct number of iterations was reached.

I've included the top line to show where it would replace the current code.

Code: Select all

/*Sort the $cities array by it's key (the distance between two cities)*/
ksort($cities);
 
$counter = 0;
foreach($cities as $distance=>$data){
    if($data['city'] != $hostcity) {
        echo "<a href='http://www.mysite.com/city/{$data['city']}'>{$value} @ $distance </a><br>";
        if(++$counter > 5) break;    
    }   
}
I've had to think a little bit about the logic you had used in your code, but I think this should suffice. I think you just wanted to skip any row that was the actual destination city and show the same output when anything was displayed.

Just as an aside, your array building method may overwrite other cities if they happen to have the same distance .... had you considered this?

Should be an easy enough fix though. Give a shout if you need help with that.

Cheers.

Re: Unable to print from a multidimensional array

Posted: Wed Jul 23, 2008 9:18 pm
by interweave34
Thanks Strykes. That helps me out cause I wasn't quit sure how to break the foreach loop. I'll try working that into my code.