How to use ORDER BY for a data that does not come from the Bank?

Asked

Viewed 57 times

0

How to use ORDER BY for a data that does not come from db? ex:

$query = $mysql->query("SELECT * FROM db_aux ;");
while($row = $query->fetch_array())
{
    $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=";
    $url .= $row['cep_local']."&destinations=".$row['cep_user'];
    $url .= "&language=pt&mode=car&key=AIzaSyDL6_dJ-Mbi_03_g6lHhWibxY22Z2UeYZQ";
    $json = file_get_contents($url);
    $json_data = json_decode($json, true);  
}

I want to order by this data that comes from the consultation json, DESC

 $json_data['rows'][0]['elements'][0]['distance']['text'];
  • in his $json_data has several lines? as would be an example of the $json_data???

  • https://maps.googleapis.com/maps/api/distancematrix/json?origins=86830-000&destinations=86015-810&language=pt&mode=car&key=AIzaSyDL6_dJ-Mbi_03_g6lHhWibxY22Z2UeYZQ

  • { "destination_addresses" : [ "Nova Londres, Londrina - PR, 86015-810, Brasil" ], "origin_addresses" : [ "Rio Bom - PR, 86830-000, Brasil" ], "Rows" : [ { "Elements" [ { "Distance" { "text" "111 km", "value" 111283 }, "Duration" { "text" : "1 hour 58 min.", "value" : 7101 }, "status" "OK" } ] } ], "status" "OK" }

  • In case it is an item you want to order?

  • I want to order by distance

  • In the example case if you have a $json_data['rows'][0]['elements'][0]['distance']['text'] my question is? how many items have these because an item does not need to be ordered!? correct

  • Yes he gives o while and comes all bank ceps in case some 50, I want to order from minor to major

  • because your code isn’t wrong? shouldn’t it be on the last line $json_data[] = json_decode($json, true); ???

  • it’s working normal just doesn’t order

  • Have you seen the API documentation to see if there is no parameter you can pass in the query that returns already ordered data? Making this type of customer-side operation is quite costly at the performance level.

  • @Joãopedromorais in the ultma line doesn’t have to be a array?

  • No poblema, will be used on a panel, not directly to the customer

  • I do not understand very well of json but always use as this there, only that I never needed to order or receive more than 1 dice

Show 8 more comments

1 answer

1


With some modifications to your code and using the function usort if you have a sort by the desired field, example:

<?php
    $item = array(
        array('86015-810', '86830-000'),
        array('86015-820', '86840-000'),
    );
    for($i = 0; $i < count($item); $i++)
    {
        $row = $item[$i];
        $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=";
        $url .= "{$row[0]}&destinations={$row[1]}";
        $url .= "&language=pt&mode=car&key=AIzaSyDL6_dJ-Mbi_03_g6lHhWibxY22Z2UeYZQ";
        $json = file_get_contents($url);
        $json_data[] = json_decode($json, true);
    }


    function sort_text($a, $b) {
        $f0 = str_ireplace([' ', 'km',','], ['','','.'],
                             $a['rows'][0]['elements'][0]['distance']['text']);
        $f1 = str_ireplace([' ', 'km',','], ['','','.'], 
                             $b['rows'][0]['elements'][0]['distance']['text']);

        return $f0 > $f1;
    }

    usort($json_data, 'sort_text');

    echo '<pre>';
    print_r($json_data);
    echo '</pre>';

Observing: if you can use that while of the question, but, the variable has to be an array that in this case is the $json_data to store all the results and then do the ordering.

  • Your code tells me that "this site I use only for proper tests" https://devjp.xyz/teste.php I’ll set an example for you to see what I need, I don’t know if I understand well

  • Ve ai https://devjp.xyz/cep.php I want to sort this list

  • @Joãopedromorais was not to return ordered?

  • I didn’t understand the way you did how I’m going to present 1 die at a time inside a div

  • I couldn’t give while along with the data coming from db

  • You are using PDO or MySqli???

  • I am using Mysqli

  • @Joãopedromorais if made this change: $json_data[] = json_decode($json, true); ???

  • Look I don’t understand how you do, for now I’ll make it be sent to the bank there I search again and order in connection!

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.