-1

This is a code of a certain rating, the problem is that when a person is added who has the same number of points as the past, he takes first place. For example: I got 5 points and took the first place, when after me you got the same 5 points, you get the first place. And I need to keep the time of receipt. I got it first means my place is first.

function game_plugin_function()
{

global $wpdb;

$table_group = $wpdb->prefix . "bp_groups"; // group
$table_members = $wpdb->prefix . "bp_groups_members"; // group members
$table_matauser = $wpdb->prefix . "usermeta"; // user meta

$groups_array = array();

$sql_groups = $wpdb->get_results("SELECT * FROM $table_group");
foreach($sql_groups as $sql_groups_new)
{
    $a_group_id = $sql_groups_new->id;
    $a_group_name = $sql_groups_new->name;

    $sql_members = $wpdb->get_results("SELECT * FROM $table_members where group_id = '$a_group_id'");
    foreach($sql_members as $sql_members_new)
    {
        $a_member_id = $sql_members_new->user_id;

        $sql_pointer = $wpdb->get_results("SELECT meta_value FROM $table_matauser where user_id = '$a_member_id' AND meta_key = '_gamipress_star_points' ");
        foreach($sql_pointer as $sql_pointer_new)
        {
            $a_groups_points = $sql_pointer_new->meta_value;
            $groups_array[$a_group_name] += $a_groups_points;
        }
    }
}
array_filter($groups_array);
arsort($groups_array);
$icount_rank = 0;
?>
<table>
    <tr>
        <th>
            Rank
        </th>
        <th>
            Group    
        </th>
        <th>
            Points
        </th>
    </tr>
    <?php
    foreach($groups_array as $groups_array_title => $groups_array_points)
    {
        $icount_rank++;
        echo '<tr>';
            echo '<td>';
                echo $icount_rank;
            echo '</td>';
            echo '<td>';
                echo $groups_array_title;
            echo '</td>';
            echo '<td>';
                echo $groups_array_points;
            echo '</td>';
        echo '</tr>';
    }
    ?>
</table>
<?php  
}
?>

I want to add something like this here:

$sql_date = $wpbd->get_results("SELECT $date FROM $table_date where user_id = '$a_member_id' AND trigger_type = 'gamipress_earn_points'");

To add something like a filter by date, how to implement this? I hope you understand me

nikaose
  • 1
  • 1

2 Answers2

0

I think the simplest way is adding order by to your query. Example: SELECT * FROM table ORDER BY points DESC, date DESC or SELECT * FROM table ORDER BY points DESC, id DESC if you have auto increment id it should work too.

Seldo97
  • 611
  • 1
  • 8
  • 17
  • And what's the difference if the bottom of the "array_filter" and "arsort" sort this table anyway? Explain, please. – nikaose Apr 17 '20 at 20:28
  • Why you try sort it in PHP if can declare sorting order in very simple way in sql query? – Seldo97 Apr 17 '20 at 20:38
  • I am not good at this code and don’t know how to implement it. I need a solution. Also, several databases are used here and not one. – nikaose Apr 17 '20 at 20:43
  • Write me what column you have in table, which column do you wanna display and how you want sort it. – Seldo97 Apr 17 '20 at 20:53
  • I want show column - name from table - bp_groups it's teams. and also, we have users who have points, they are in a group and we check people who are in a group and summarize their rating. Next, we display the rating type in the table. – nikaose Apr 17 '20 at 21:13
  • Yes, it's possible? – nikaose Apr 17 '20 at 21:18
  • Yes. `SELECT name FROM bp_groups ORDER BY points DESC, date DESC` – Seldo97 Apr 17 '20 at 21:27
  • It should be more easier if you send your full structure of database. – Seldo97 Apr 17 '20 at 22:13
-1
$sql_date = $wpbd->get_results("SELECT * FROM table_date where user_id = '$a_member_id' AND trigger_type = 'gamipress_earn_points' ORDER BY datetime_field DESC ");
  • And what's the difference if the bottom of the "array_filter" and "arsort" sort this table anyway? Explain, please. – nikaose Apr 17 '20 at 20:28
  • 1
    Please add some explanation to your answer such that others can learn from it. Also explain why you post a query that is open to SQL injection – Nico Haase Apr 18 '20 at 09:35