powelly Posted May 4, 2007 Share Posted May 4, 2007 This is driving me nuts I just cant seem to get my head around it. I run a query to select some information from a table, then I loop through each of the records returned performing a calculation on one of the fields. I need to display the results sorted by the result of the calculation so I need to insert the returned results into an array. //fill array $Location_Distances_md_array[] = array($row[1] => $distance); then once the loop finishes I want to display the content of the array sorted by the $distance, but Im lost here, I just cant get my head around it. This is what I have so far <? include 'db.php' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?php // to use this grab the SQL database of postcodes from www.pjenkins.co.uk/uk_postcodes function getDistance($lat1, $long1, $lat2, $long2) { //$earth = 6371; //km change accordingly $earth = 3960; //miles //Point 1 cords $lat1 = deg2rad($lat1); $long1= deg2rad($long1); //Point 2 cords $lat2 = deg2rad($lat2); $long2= deg2rad($long2); //Haversine Formula $dlong=$long2-$long1; $dlat=$lat2-$lat1; $sinlat=sin($dlat/2); $sinlong=sin($dlong/2); $a=($sinlat*$sinlat)+cos($lat1)*cos($lat2)*($sinlong*$sinlong); $c=2*asin(min(1,sqrt($a))); $d=round($earth*$c); return $d; } if($_POST) { mysql_connect($host,$user,$pass); @mysql_select_db($db) or die( "Unable to select database"); $firstpc = strtoupper(preg_replace("/[^a-zA-Z0-9]/","", $_POST[first])); $secondpc = strtoupper(preg_replace("/[^a-zA-Z0-9]/","", $_POST[second])); $query = 'SELECT `Lat`, `Long` FROM `UKPostCodes` WHERE `PostCode`="'.$firstpc.'";'; $result = mysql_query($query); $first = mysql_fetch_row($result); // set database server access variables: // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // create query $query = "SELECT * FROM `Locations`;"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { // yes // print them one after another while($row = mysql_fetch_row($result)) { echo "<tr>"; $query2 = 'SELECT `Lat`, `Long` FROM `UKPostCodes` WHERE `PostCode`="'.$row[2].'";'; $result2 = mysql_query($query2); $second = mysql_fetch_row($result2); //calculate distance $distance = getDistance($first[0], $first[1], $second[0], $second[1])." miles."; //fill array $Location_Distances_md_array[] = array($row[0] => $distance); //echo data echo $row[0]." ".$row[1]." ".$row[3]." ".$distance."<br>"; } } else { // no // print status message echo "<p align='center'><font color='#808080' face='Arial'>No records found.</font></p> "; } mysql_close(); echo "<br>"; } for ( $row = 0; $row < 10; $row++ ) { for ( $column = 0; $column < 10; $column++ ) { echo $Location_Distances_md_array[$row][$column]; } echo '<br />'; } ?> <form action="index.php" method="post"> Only enter the first part of the postcode. If your postcode is TS20 3DS, just enter the TS20.<br /><br /> First Postcode: <input name="first" maxlength="4" /><br /> <br /> <input type="submit" /> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/49951-multidimensional-arrays-for-newbies/ Share on other sites More sharing options...
Barand Posted May 4, 2007 Share Posted May 4, 2007 Can you post the output from var_export($Location_Distances_md_array); Link to comment https://forums.phpfreaks.com/topic/49951-multidimensional-arrays-for-newbies/#findComment-245252 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.