Можно ли отсортировать этот вывод по «количеству ($ getaffreferrals)»?

Возможно ли отсортировать вывод $ closestring по «count ($ getaffreferrals)»?

Я приложил фотографию, чтобы показать, что она в настоящее время выводит на моем сайте. Снимок экрана с выводом

function affiliate_leaderboard_function() {
global $wpdb;

$getallreferrals = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_referrals`");
$getallaffiliates = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_affiliates`");
$current_month = date("m");
$current_year = date("Y");
$current_date = date("Y-m-d");
$lastday = date('t',strtotime($current_date));

function styleleaderboard() {
?>
<style>
span.numberofsales {
font-size:18px !important;
color : #666 !important;
}
.closerstring {
text-transform: capitalize;
}
</style>
<?php
}
add_action('wp_head', 'styleleaderboard');
styleleaderboard();

foreach ($getallaffiliates as $theaffiliate) {
$user_id = get_userdata( $theaffiliate->user_id );
$userfirstname = $user_id->first_name;
$userlastname = $user_id->last_name;
$totalreferrals = $theaffiliate->referrals;
$affiliate_id = $theaffiliate->affiliate_id;
$getaffreferrals = $wpdb->get_results( "SELECT `date` FROM `wp_affiliate_wp_referrals` WHERE `affiliate_id` = $affiliate_id AND `date` >= '$current_year-$current_month-01:00:00:00' AND `date` < '$current_year-$current_month-$lastday:23:59:59'");//Get all referrals by affiliate id
$closerstring = "<li class='closerstring'>".$userfirstname." ".$userlastname."<br><span class='numberofsales'>Sales: ".count($getaffreferrals)."</p></li>";
if(!empty($getaffreferrals) && $affiliate_id!='1'){
$return_array.= $closerstring;
}
}
return $return_array;
}
add_shortcode('affiliate_leaderboard' , 'affiliate_leaderboard_function');

-1

Решение

Возможно ли отсортировать вывод $ closestring по «count ($ getaffreferrals)»?

Я приложил фотографию, чтобы показать, что она в настоящее время выводит на моем сайте. Снимок экрана с выводом

function affiliate_leaderboard_function() {
global $wpdb;

$getallreferrals = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_referrals`");
$getallaffiliates = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_affiliates`");
$current_month = date("m");
$current_year = date("Y");
$current_date = date("Y-m-d");
$lastday = date('t',strtotime($current_date));

function styleleaderboard() {
?>
<style>
span.numberofsales {
font-size:18px !important;
color : #666 !important;
}
.closerstring {
text-transform: capitalize;
}
</style>
<?php
}
add_action('wp_head', 'styleleaderboard');
styleleaderboard();

foreach ($getallaffiliates as $theaffiliate) {
$user_id = get_userdata( $theaffiliate->user_id );
$userfirstname = $user_id->first_name;
$userlastname = $user_id->last_name;
$totalreferrals = $theaffiliate->referrals;
$affiliate_id = $theaffiliate->affiliate_id;
$getaffreferrals = $wpdb->get_results( "SELECT `date` FROM `wp_affiliate_wp_referrals` WHERE `affiliate_id` = $affiliate_id AND `date` >= '$current_year-$current_month-01:00:00:00' AND `date` < '$current_year-$current_month-$lastday:23:59:59'");//Get all referrals by affiliate id
$closerstring = "<li class='closerstring'>".$userfirstname." ".$userlastname."<br><span class='numberofsales'>Sales: ".count($getaffreferrals)."</p></li>";
if(!empty($getaffreferrals) && $affiliate_id!='1'){
$return_array.= $closerstring;
}
}
return $return_array;
}
add_shortcode('affiliate_leaderboard' , 'affiliate_leaderboard_function');
самый старый «data-shortcut =» O

Другие решения

Если вы помещаете данные в многомерный массив, одно поле — строка, а другое — поле, в которое вы хотите отсортировать, вы можете использовать PHP array_multisort для сортировки по этому ключу. Я не тестировал этот код, но он должен выглядеть примерно так:

function affiliate_leaderboard_function() {
global $wpdb;

$getallreferrals = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_referrals`");
$getallaffiliates = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_affiliates`");
$current_month = date("m");
$current_year = date("Y");
$current_date = date("Y-m-d");
$lastday = date('t',strtotime($current_date));

function styleleaderboard() {
?>
<style>
span.numberofsales {
font-size:18px !important;
color : #666 !important;
}
.closerstring {
text-transform: capitalize;
}
</style>
<?php
}
add_action('wp_head', 'styleleaderboard');
styleleaderboard();

$my_array = array();
foreach ($getallaffiliates as $theaffiliate) {
$user_id = get_userdata( $theaffiliate->user_id );
$userfirstname = $user_id->first_name;
$userlastname = $user_id->last_name;
$totalreferrals = $theaffiliate->referrals;
$affiliate_id = $theaffiliate->affiliate_id;
$getaffreferrals = $wpdb->get_results( "SELECT `date` FROM `wp_affiliate_wp_referrals` WHERE `affiliate_id` = $affiliate_id AND `date` >= '$current_year-$current_month-01:00:00:00' AND `date` < '$current_year-$current_month-$lastday:23:59:59'");//Get all referrals by affiliate id
$getaffreferrals_count = count($getaffreferrals);
$closerstring = "<li class='closerstring'>".$userfirstname." ".$userlastname."<br><span class='numberofsales'>Sales: ".$getaffreferrals_count."</p></li>";
if(!empty($getaffreferrals) && $affiliate_id!='1'){
$my_array[] = array('affreferrals' => $getaffreferrals_count, 'closerstring' => $closerstring);
//$return_array.= $closerstring;
}
}

// Obtain a list of columns
$referrals = array();
$closerstr = array();
foreach ($my_array as $key => $row) {
$referrals[$key] = $row['affreferrals'];
$closerstr[$key] = $row['closerstring'];
}

// Sort the data with affreferrals descending, closerstring ascending
// Add $my_array as the last parameter, to sort by the common key
array_multisort($referrals, SORT_DESC, $closerstr, SORT_ASC, $my_array);

$return_array = '';
foreach ($my_array as $row) {
$return_array .= $row['closerstring'];
}

return $return_array;
}
add_shortcode('affiliate_leaderboard' , 'affiliate_leaderboard_function');
голосует «data-shortcut =» V

0