Array Woes [SOLVED]
Posted: Wed Aug 24, 2005 8:23 pm
Consider the following piece of code which behaves EXACTLY as I want it to.
Result:
Perfect! The problem lies that this will loop xx amount of times, depending on the number of elements in the array... which can take several minutes in some cases. So I've been trying to do it in one query.. here's what I have so far.
The problem with this piece of code is I do not know how to order the results by distance (which would be the key of the Example $zips array), and I don't know how to show the distance (the value of $key in the Example $zips array). Some help please. =)
I hope I explained this as clearly as possible
Code: Select all
// Example $zips array [12345] => 1.235, [98547] => 2.514, [51485] =>3.214, [15845] => 9.5485
// Each key is a zip code, and each key value is a number in miles
// The array is sorted so that the value goes from smallest, to biggest
foreach ($zips AS $key => $value)
{
$query = mysql_query("SELECT username FROM users WHERE zip = '$key' ORDER BY username ASC");
while($array = mysql_fetch_assoc($query))
{
echo "<B>User:</B> ".$array['username']." - <B>Distance:</B> $value miles.<BR>";
}
}Code: Select all
User: cuddles - Distance: 1.47 miles.
User: jessipoo52 - Distance: 1.47 miles.
User: quinnbee2000 - Distance: 3.19 miles.
User: lilmissolivia06 - Distance: 9.45 miles.
User: d152guy - Distance: 16.61 miles.
User: shylilsweetie986 - Distance: 25 miles.Code: Select all
// Example $zips array [12345] => 1.235, [98547] => 2.514, [51485] =>3.214, [15845] => 9.5485
// Each key is a zip code, and each key value is a number in miles
// The array is sorted so that the value goes from smallest, to biggest
$ziparray = array_keys($zips); // returns array of ZIP codes only
$newzips = implode(",",$ziparray); // returns a comma delimited list
$query = mysql_query("SELECT username FROM users WHERE zip IN ($newzips)") or die(mysql_error());
while($array = mysql_fetch_assoc($query))
{
echo "<B>User:</B> ".$array['username']."<BR>";
}I hope I explained this as clearly as possible