按照数组的形式排序+条件切换切换查询

    //TOP50
  public static function getTOP50 ($fromTime='',$endTime='',$type='warAllNum' ) {
   $fromTime = str_replace("-", "", substr($fromTime, 0, 10));
   $endTime = str_replace("-", "", substr($endTime, 0, 10));
   $data = self::getDayCount($fromTime,$endTime);
   $type_A = array('warAllNum','warWinNum','warFailNum','killNum','deadNum') ;
   if(!in_array($type,$type_A)) return false;
                 
   $result = array_column_sort($data, "$type", $sortDirection = SORT_DESC) ;
   $result = array_slice($result,0,50);
   return $result;
  }


  -----------------------------------

  按照$type进行排序,$type是array格式,
    $type_A = array('warAllNum','warWinNum','warFailNum','killNum','deadNum') ;
   if(!in_array($type,$type_A)) return false;             
   $result = array_column_sort($data, "$type", $sortDirection = SORT_DESC) ;//按照$type排序


   ==========================================================================================
   
   ----------------------------------------------------------------------------


 

public static function showbattleresult($ifuser = 1, $starttime = 0, $endtime = 0, $ifwin = 1){
  $returnarr = array();//提前定义可以提高执行效率
  $sqladd= "";
  $column = $ifuser ? "userid" : "union_id";
  $starttime && $sqladd = " and time >=" . $starttime;
  $endtime && $sqladd .= " and time <=" . $endtime;
  $sql = "SELECT $column, activearmy_id, count( * )  as num
    FROM `tb_log_activearmy` 
    WHERE outcome=" . $ifwin . " " . $sqladd . "
    GROUP BY $column, `activearmy_id`";
//  return $sql;
  $ret = mysql_query($sql);
  while($row = mysql_fetch_assoc($ret)){
   $returnarr[$row[$column]][$row['activearmy_id']] = $row['num'];
   $returnarr[$row[$column]]['total'] = isset($returnarr[$row[$column]]['total']) ? $returnarr[$row[$column]]['total'] + $row['num'] : $row['num'];
   $returnarr[$row[$column]]['id'] = $row[$column];
  }
  return $returnarr;
 }
-------------------------------------------前提:表里面同时有这两个字段,切换用户id,和联盟id:$column = $ifuser ? "userid" : "union_id";三元目运算

posted @ 2011-03-11 16:29  y0umer  阅读(196)  评论(0编辑  收藏  举报