Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-如何从查询中获取订单并获取;地点编号“;_Php_Mysql_Sql_Pdo - Fatal编程技术网

PHP-如何从查询中获取订单并获取;地点编号“;

PHP-如何从查询中获取订单并获取;地点编号“;,php,mysql,sql,pdo,Php,Mysql,Sql,Pdo,我如何从表中选择前40名数据(基于金额),然后找到特定用户在该数据组中的位置 例如,我选择了前40个数据: if($userdata['forum_moderator'] == 1){ $c = $dbh->prepare("SELECT * FROM referral_competition ORDER BY amount DESC LIMIT 5"); $c->execute(); $competition = $c->fetchAll(); echo "<

我如何从表中选择前40名数据(基于
金额
),然后找到特定用户在该数据组中的位置

例如,我选择了前40个数据:

if($userdata['forum_moderator'] == 1){
$c = $dbh->prepare("SELECT * FROM referral_competition ORDER BY amount DESC LIMIT 5");
$c->execute();

$competition = $c->fetchAll();

    echo "<pre>";
    var_dump($competition);
    echo "</pre>";
如何根据用户id找出特定用户在上面的数组中的位置

例如:


您被放置在第3位,并且您在内部有
amount:8

选择获取前40名并向其添加排名列。另一个select仅获取特定用户的排名和金额

  $ordered = [];
  foreach($competition as $k=>$result) {
    $rank = $k + 1; //Add plus one as the indexed resultset starts from zero
    $user_id = $result['userid']; //Grab the user id
    $ordered[$user_id]=$result; //Little trick to add the user id as the array index key
    $ordered[$user_id]['rank']=$rank; //Add the rank as new array element
  }

  //Now you can get any user record out of the array by using the userid as key

  //Example:
  $user = $ordered[3];
  $rank = $user['rank'];

此解决方案不需要您更改已有的sql查询。它还使您能够快速访问用户数据


谢谢,现在当我执行var_dump()时,它会显示正确的“顺序”。不过,当我试着回应时:什么也没显示。对不起,我帮不了你。这似乎是一个PHP问题,我不是这方面的专家。为什么不简单地使用$competition的数组键并添加1?您所说的“添加1”是什么意思?你能举个例子吗?用户名为4的用户是$competition[2]的元素,因此她将数字2+1=3…@VMai,但我该怎么做呢?比如:$compatitions[$userid]+1?我不知道你用这个数组做什么,所以我不能说。juergen d解决方案的好处是秩包含在数组元素中,而不依赖于数组本身。处理此数组的部分不知何故丢失了。
select amount, rank 
from 
(
  SELECT *, @rank := @rank + 1 as rank
  FROM referral_competition 
  CROSS JOIN (select @rank := 0) r
  ORDER BY amount DESC 
  LIMIT 40
) tmp
where userid = $someUser
  $ordered = [];
  foreach($competition as $k=>$result) {
    $rank = $k + 1; //Add plus one as the indexed resultset starts from zero
    $user_id = $result['userid']; //Grab the user id
    $ordered[$user_id]=$result; //Little trick to add the user id as the array index key
    $ordered[$user_id]['rank']=$rank; //Add the rank as new array element
  }

  //Now you can get any user record out of the array by using the userid as key

  //Example:
  $user = $ordered[3];
  $rank = $user['rank'];