RE: [PHP] Array craziness - Someone plese help

From: Date: Sun, 06 Apr 2003 18:51:06 +0000
Subject: RE: [PHP] Array craziness - Someone plese help
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-142606@lists.php.net to get a copy of this message
I don't understand what you want the result to be. Can you give an example? Do you just want to keep track of all the $totaldist results? If so: $totaldist[] = distance($line1[0],$line1[1],$line2[0],$line2[1],"mi"); ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ > -----Original Message----- > From: Vernon [mailto:vernon@comp-wiz.com] > Sent: Sunday, April 06, 2003 2:43 PM > To: php-general@lists.php.net > Subject: [PHP] Array craziness - Someone plese help > > 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. > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.general (#142606) next »