2011-07-24 50 views
0

我試圖使用ORDER BY DESC結果排序,但結果被foreach值排序:有兩個表PHP排序值導致

while($row = mysqli_fetch_array($sqlgroup)){ 
    $member_array = $row["member_array"]; 

    if ($member_array !=""){ 
     $memberArray = explode(",", $member_array); 
     $i = 0; 
     $cashstatsList .= ' 
     '; 
     foreach($memberArray as $gkey => $mvalue){ 
      $i++; 

      $arraystats = "SELECT player.first_name, player.last_name, SUM(groupcash.grpcsh_earnings) AS memsum, AVG (groupcash.grpcsh_earnings) AS memavg, 
          SUM(groupcash.grpcsh_w) AS memcntpos, SUM(groupcash.grpcsh_l) AS memcntneg 
          FROM player, groupcash 
          WHERE (player.id = grpcsh_plrid) AND (player.id = $mvalue) AND (groupcash.grpcsh_groupid = $groupid)           
          AND (grpcsh_date >= '$thisyr') AND (grpcsh_date <= '$today') ORDER BY SUM(groupcash.grpcsh_earnings) DESC        
          "; 

      $arraystatsResutls = mysqli_query($link, $arraystats); 
      if (!$arraystatsResutls){ 
       $cashstatsList .= '       
        <tr> 
         <td>        
          No results available for listed dates 
         </td> 
        </tr>'; 
      } else { 
       while($row = mysqli_fetch_array($arraystatsResutls)){ 
        $memberFirstName = $row["first_name"]; 
        $memberLastName = $row["last_name"]; 
        $sum = $row["memsum"]; 
        $avg = $row["memavg"]; 
        $win = $row["memcntpos"]; 
        $loss = $row["memcntneg"]; 

        if ($memberFirstName == "" || $memberLastName == ""){ 
         $sqlName = mysqli_query($link, "SELECT first_name, last_name FROM player WHERE id='$mvalue' LIMIT 1") or die ("Sorry we had a mysql error!"); 
          while ($row = mysqli_fetch_array($sqlName)) { 
           $memberFirstName = $row["first_name"]; $memberLastName = $row["last_name"]; 
          }           
        } 

        if ($sum == ""){ 
         $sum = "0"; 
        } 
        if ($avg == ""){ 
         $avg = "0"; 
        } 
        if ($win == ""){ 
         $win = "0"; 
        } 
        if ($loss == ""){ 
         $loss = "0"; 
        } 

        $cashstatsList .= ' 
         <tr align="center"> 
          <td> 
           ' . $i . ' 
          </td> 
           <td> 
           ' . $memberFirstName . '&nbsp;' . $memberLastName . ' 
          </td> 
          <td> 
           $' . $sum . ' 
          </td> 
          <td> 
           $' . $avg . ' 
          </td> 
          <td> 
           ' . $win . '/' . $loss . ' 
          </td> 
         </tr>'; 
       } 
      } 
     } 
    } 
} 
+0

什麼是第一個查詢被循環 – Ibu

回答

0

你必須在查詢一次選擇該組的所有成員,以便他們可以是「ORDER BY」,在這裏你只對每個請求的同一個成員有結果,所以這個順序不會改變成員之間結果的順序。

如果$member_array就像1,454,33,22你可以只取出的foreach和使用查詢:

$arraystats = "SELECT player.first_name, player.last_name, SUM(groupcash.grpcsh_earnings) AS memsum, AVG (groupcash.grpcsh_earnings) AS memavg, 
       SUM(groupcash.grpcsh_w) AS memcntpos, SUM(groupcash.grpcsh_l) AS memcntneg 
       FROM player, groupcash 
       WHERE (player.id = grpcsh_plrid) AND (player.id IN ($member_array)) AND (groupcash.grpcsh_groupid = $groupid)           
       AND (grpcsh_date >= '$thisyr') AND (grpcsh_date <= '$today') ORDER BY SUM(groupcash.grpcsh_earnings) DESC        
       "; 

所以你會得到在同一時間組和訂單的所有會員將工作。