mysql - PHP loop x number of times, using while and if -


so has been driving me nuts several days. i've tried this , that nothing working. here's basic rundown:

i have mysql database 200 or locations. user enters zip code, , want give them 6 random locations within 100 miles of home. works great, except can't limit results 6. give me results <100. when try code below (adding for ($x=0...) bit), same result, each 1 repeats 6 times before listing next one. here's pertinent code returns locations. save me throwing computer out window.

$sql = "select * locations order rand()";  $result = mysql_query($sql, $dbase2);  while ($row = mysql_fetch_array($result))     {  $city = $row['city'];  $id= $row['id']; $miles = calculatedistance($lat, $lon, $point2_lat,$point2_lon);     if ($miles < "100")       { ($x=0; $x<6; $x++){        echo $city . " " . round($miles) . " miles away.<br />";          };       };              }; 

like said, sake of post, tried pare down important bits. let me know if vital missing example.

the following change should it:

$count = 0; while (($row = mysql_fetch_array($result)) && ($count < 6))     {      $city = $row['city'];      $id= $row['id'];     $miles = calculatedistance($lat, $lon, $point2_lat,$point2_lon);       if ($miles < 100.0) {        echo $city . " " . round($miles) . " miles away.<br />";        $count = $count + 1;       };              }; 

there more elegant ways...


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -