Support

Account

Home Forums Add-ons Repeater Field Sorting data from api

Solving

Sorting data from api

  • I use the advanced custom fields pro plugin on my wordpress site. I want to sort numerically from bigger to smaller, data which I got from API. I am receiving the data without any problems. But I could not sort.

    <?php if( have_rows('g_pool' , 'options') ): ?>
        <?php while( have_rows('g_pool' , 'options') ) : the_row(); ?>  
        <?php 
          $name = get_sub_field('pool_id'); 
    
    $o1 = file_get_contents('JSONADDRESS' . get_sub_field('pool_name')  . '?api_key=' . $api); 
        $pl1 = json_decode($o1);
        $d1 = $pl1->id;
        $a1 = file_get_contents('JSONADDRESS'. $d1 . '?api_key=' . $api);
        $player1 = json_decode($a1);
        $rank1 = $player1['0']->rank;
        $point1 = $player1['0']->leaguePoints;
        $tier = $player1['0']->tier;   
    
     $ary = ARRAY($point1);
        RSORT($ary );
        
      FOREACH ($ary AS $_point){
        ECHO $_point.'<br>';
      }    

    I cannot adjust the order from large to small. It is listing as I listed it on the panel.

  • I can’t really say, I can’t tell by looking at this what you are trying to sort. It is also not really ACF that you’re looking for help with but sorting an array (that I don’t understand by looking at the code) that you are getting from somewhere else.

    I might be able to point you in the right direction if I can understand what the array looks like that you’re trying to sort and how you’re trying to sort it.

  • @hube2 I’m sorry for not fully explaining.I will detail it right away. The sample json content I got from api is as follows:
    apidata
    As you can see there is a value called leaguePoints. In my codes above, I transfer the leaguePoints value I got from json to the point1 variable.

    The screenshot you see below is my repeater content:
    admin
    Let me explain the pool name field. Users are registered with these names in the system where I receive data with API. That’s why I write the user name they registered in the system into this pool_name field. I get the user data by assigning the value entered in the field to the api url in the codes you see above.

    And the result is as in the screenshot I shared below:
    output

    I get results as I wrote from the admin panel. All I want is to sort the numbers in the variable point1 in descending order.
    Of course, this data is demo. Maybe hundreds of lines will be entered, so I want to use repeaters. Actually I can print without sorting. But the important thing for me is to print in order.

    Thank you in advance for your help.

    If you can’t see the screenshots, the links are:
    https://prnt.sc/vylsf6 (json)
    https://prnt.sc/vylui6 (admin)
    https://prnt.sc/vyls9o (output)

  • Sorry about taking so long to get back to this, work has been crazy.

    The only thing that I can really point to us theis https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/ which is an example of how to sort repeater fields.

    You might also want to look at php usort() https://www.php.net/manual/en/function.usort.php

    I can tell you that if I was going to be dealing with data on the order of hundreds of rows that I would not be using a repeater fields. Repeater fields are not really scalable to this degree. I would probably be using a custom post type or some other mechanism for storing this data that would allow easier querying and sorting.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.