0

How to limit the output of data in the table to 10? I have a plugin for WordPress, this is a code of a certain rating:

$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 would not want to change something in the database, since I need to display both all the data and a limited amount, in my case 10.

nikaose
  • 1
  • 1

2 Answers2

0

SQL supports a LIMIT clause that allows you to limit "the output" (rows).

Usage

$sql_groups = $wpdb->get_results("SELECT * FROM $table_group LIMIT 10"); // LIMIT 10
Jonathan Rosa
  • 701
  • 3
  • 19
0

use limit in your query after where condition , limit 10 to display 10 records and limit -1 to display all records.

Ravi
  • 284
  • 2
  • 8