Array craziness - Someone plese help
| From: | Vernon | Date: | Sun, 06 Apr 2003 18:42:37 +0000 |
| Subject: | Array craziness - Someone plese help | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-142605@lists.php.net to get a copy of this message | ||
This is a bit long so I hope that some one will take a moment or tow to try
and help me with this as I, at this moment, have very little hair left.
I am going crazy trying sort a database based on a variable that is created
from the database. First I connect to the database. Then I create a do >
while loop that calculates a value from a defined value (zip1) and a value
from the database (zip2). This value is only created by doing the do > while
loop. It was suggested that I create an array an put the value in the array,
but how do I add the value that is created on the fly into this array?
This is what I am using to create the calculated distances:
mysql_select_db($database_connPENPALS, $connPENPALS);
$query_rsUSERS = "SELECT zip FROM penpals";
$rsUSERS = mysql_query($query_rsUSERS, $connPENPALS) or die(mysql_error());
$row_rsUSERS = mysql_fetch_assoc($rsUSERS);
$totalRows_rsUSERS = mysql_num_rows($rsUSERS);
do {
//set $zip2 from recordset
$zip2 = $row_rsUSERS['zip'];
//do some stuff from the distance function
$coor1=mycoors($zip1);
$coor2=mycoors($zip2);
$line1=split("\|",$coor1);
$line2=split("\|",$coor2);
//return the total distance
$totaldist=distance($line1[0],$line1[1],$line2[0],$line2[1],"mi");
} while ($row_rsUSERS = mysql_fetch_assoc($rsUSERS));
I've tried to rewrite the script using the following but cannot figure out
how am going to be able to add the $totaldist to the array as a way of
sorting it.
//db connectivity
$query_rsZIP = "SELECT zip FROM penpals";
$result = mysql_query($query_rsZIP, $connPENPALS);
while($row = mysql_fetch_row($result)) {
$myArray[$row[0]] = $row[1];
//Sort an array by key
ksort ($myArray);
reset ($myArray);
//display records
while(list($key, $val) = each($myArray)) {
echo "$key<br>";
}
I have found that you can use the:
uksort ($a, "cmp");
instead of ksort to "Sort an array by keys using a user-defined comparison
function" but cannot figure out how I will add that "user-defined comparison
function" to the array if it is constantly changing based on the recordset
and it calculations of these two variables.