Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 686301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:00:38+00:00 2026-05-14T02:00:38+00:00

I am trying to output results of 2 sql queries to one JSON file.

  • 0

I am trying to output results of 2 sql queries to one JSON file. The problem is that I would like to order them ascending by distance which is the result of equation that takes homelat and homelon from the users table and lat, lng from locations table.(basically it takes lattitude and longitude of one point and another and computes the distance between these points). Is it possible to take some parameters from both select queries, compute it and output the result in ascending order?

$wynik = mysql_query("SELECT homelat, homelon FROM users WHERE guid='2'") or 
die(mysql_error()); ;

$query = "SELECT * FROM locations WHERE timestamp";
$result = map_query($query);
$points = array();

while ($aaa = mysql_fetch_assoc($wynik)) {

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

    array_push($points, array('name'=>$row['name'], 'lat'=>$row['lat'], 
'lng'=>$row['lng'], 'description'=>$row['description'], 
'eventType'=>$row['eventType'], 'date'=>$row['date'],
        'isotime'=>date('c', ($row['timestamp'])), 'homelat'=>$aaa['homelat'], 
'homelon'=>$aaa['homelon']));

}
echo json_encode(array("Locations"=>$points));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:00:38+00:00Added an answer on May 14, 2026 at 2:00 am

    So the resulting array should be something like:

    'name' => 'something',
    'lat' => 'something',
    'lng' => 'something',
    'description' => 'something',
    etc.
    

    Use usort to sort it.

    usort($array, 'sortByOption');
    function sortByOption($a, $b) {
      $distA = dist($a['homelat'], $a['homelong'], $a['lat'], $a['long']);
      $distB = dist($b['homelat'], $b['homelong'], $b['lat'], $b['long']);
      return strcmp($distA, $distB);
    }
    function dist($x1, $y1, $x2, $y2) {
      return pow($x2 - $x1, 2) + pow($y2 - $y1, 2);
    }
    

    EDIT: Sorry, didn’t 100% read your question. Revised for the solution (though not a great one). You should be calculating distance in the array itself. And to save time, don’t do the square-root.

    EDIT 2: Final result.

    while ($aaa = mysql_fetch_assoc($wynik)) {
      while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        array_push($points, array('name'=>$row['name'], 
                                   'lat'=>$row['lat'], 
                                   'lng'=>$row['lng'], 
                           'description'=>$row['description'], 
                             'eventType'=>$row['eventType'],
                                  'date'=>$row['date'],
                               'isotime'=>date('c', ($row['timestamp'])), 
                               'homelat'=>$aaa['homelat'], 
                               'homelon'=>$aaa['homelon'],
                              'dist'=>dist($aaa['homelat'], $aaa['homelon'], $row['lat'], $row['lng'])
        ));
      }
    }
    usort($points, 'sortByDist'); // This sorts the points!
    echo json_encode(array("Locations"=>$points));
    function sortByDist($a, $b) {
      return strcmp($a['dist'], $b['dist']);
    }
    function dist($x1, $y1, $x2, $y2) {
      return pow($x2 - $x1, 2) + pow($y2 - $y1, 2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get my servlet to output the results of SQL commands entered
I am trying to output mysql query results into JSON format,but struggling to output
I am trying to output JSON directly in my Razor view. The object being
I'm trying to do a full join on two SQL queries, below: 1st Query:
I have the following SQL query in MS Access. I'm trying to order the
I am trying to set an array based on query results that are NOT
I am trying to make Doctrine generate SQL from a set of models that
In PHP I am having difficulty trying to extract row data from one SQL
I'm trying to output the result of my SQL query to a div using
Trying to populate select with sql results using WebMethods. I can get and pass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.