简体   繁体   English

获取 user_id 记录并计算列

[英]Get user_id records and calculate columns

Basically I have a table with dozens of records, inside I have the wordpress user_id and some columns with stats.基本上我有一个包含几十条记录的表,里面有 wordpress user_id和一些带有统计信息的列。

I need to get all the records, combine all user data and show like a rank table.我需要获取所有记录,结合所有用户数据并像排名表一样显示。

table example of how I'm storing it:我如何存储它的表格示例:

2 (wordpress user_id) / 2 / 4 / 1
3 / 4 / 1 / 2
2 / 1 / 3 / 0

I'm getting the results: $result = $wpdb->get_results("SELECT * FROM wp7s_g_stats");我得到了结果: $result = $wpdb->get_results("SELECT * FROM wp7s_g_stats");

And I'm printing in a table:我在一张桌子上打印:

<?php $i = 1; foreach($result as $column){ ?>
    <tr>
        <td><?php echo $i++."º"; ?></td>
        <td><?php $username = get_user_by( 'id', $column->stats_user_id ); echo $username->display_name; ?></td>
        <td><?php echo $column->stats_sales;?></td>
        <td><?php echo $column->stats_refunds;?></td>
        <td><?php echo $column->stats_cancellations;?></td>
    </tr>
<?php } ?>

How I want to show, where is order by sales:我想如何显示,按销售订购在哪里:

Rank 1º / username 3 / 4 / 1 / 2
Rank 2º / username 2 / 3 / 7 / 1

Can someone please advise?有人可以建议吗?

If the rest of the code is correct, which I did not test, and your model is correct, then it should be enough to sort your results by stats_sales to obtain the ranking:如果其余代码是正确的(我没有测试),并且您的模型是正确的,那么通过stats_sales对您的结果进行排序以获得排名就足够了:

$stats = $wpdb->get_results("SELECT * FROM wp7s_g_stats ORDER BY stats_sales DESC"); 

BTW, I think that $column as a variable name is confusing.顺便说一句,我认为$column作为变量名令人困惑。 Maybe $user_stat would be more fitting也许 $user_stat 会更合适

<?php $i = 1; foreach($stats as $user_stat){ ?>
    <tr>
        <td><?php echo $i++."º"; ?></td>
        <td><?php $username = get_user_by( 'id', $user_stat->stats_user_id ); echo $username->display_name; ?></td>
        <td><?php echo $user_stat->stats_sales;?></td>
        <td><?php echo $user_stat->stats_refunds;?></td>
        <td><?php echo $user_stat->stats_cancellations;?></td>
    </tr>
<?php } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM