2012-11-01 39 views
0

我有這個表中MySQL數據庫:擷取來自在PHP MySQL查詢結果數組,而循環問題

SELECT score, count(*) FROM Coaches group by score ORDER BY score DESC 

結果表如下所示:

enter image description here

運行此查詢後這樣的:

enter image description here

現在在PHP中,我嘗試獲取結果並遍歷數組,以確定每個教練屬於哪個組並獲得他在排名中的位置。因此我寫到:

$groupsOfScoresQuery = "SELECT score, count(*) FROM Coaches group by score ORDER BY score DESC"; 

$result = mysqli_query($dbc, $groupsOfScoresQuery); 

if ($result) { // query did successfully run 
$response['topCoaches'] = array(); 

    if (mysqli_num_rows($result) > 0) { 
     while ($rowScore = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 

      $currentRanking++; 
      $score = array(); // temp user array for one group of scores 
      $numberOfCoaches; // Number of coaches with this particular number of scores 
      $scoresGroup; // Scores in the particular group 

      $score["scores"] = $rowScore["score"]; 
      $score["count"] = $rowScore["count(*)"]; 
      $numberOfCoaches = $score["count"]; 
      $scoresGroup = $score["scores"]; 

      $response["scoresGroup"] = $scoresGroup; // HERE IS THE PROBLEM 

. 
. 
. 
more processing 
} // end WHILE 

爲什麼$response["scoresGroup"]將始終conatins結果的最後一個值?在這種情況下,這是。我認爲這是通過循環的第一次迭代,並且第一個元素(474)將持有第一個元素,在第二次迭代期間應該保持382?我在這裏做錯了什麼?我是否使用正確的函數來獲取結果?還是應該用不同的循環來實現我的目標?我在這裏先向您的幫助表示感謝。

回答

2

你沒有張貼$response預期的結構;這裏是我認爲你正在嘗試做的:

while ($rowScore = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 
    $response["scoresGroup"][] = array(
     "scores" => $rowScore["score"], 
     "count" => $rowScore["count(*)"] 
    ); 
} 
// $response["scoresGroup"][0]["scores"] = 474 
// $response["scoresGroup"][0]["count"] = 1 
// $response["scoresGroup"][1]["scores"] = 382 
// $response["scoresGroup"][1]["count"] = 1 
// $response["scoresGroup"][2]["scores"] = 123 
// $response["scoresGroup"][2]["count"] = 1 

或許:

while ($rowScore = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 
    $response["scoresGroup"][$rowScore["score"]] = $rowScore["count(*)"] 
} 
// $response["scoresGroup"][474] = 1 
// $response["scoresGroup"][382] = 1 
// $response["scoresGroup"][123] = 1 
1

每次運行循環時,你都會設置$ response ['scoresGroup'],所以最後它只包含最後一個元素。嘗試在每個循環中更改放入數據的變量。

$x++; 
$response['scoresGroup' . x] = $scoresGroup; 
+0

感謝您的答覆。所有的答案都是正確的+1我接受了最後一個。 –

2
if (mysqli_num_rows($result) > 0) { 
     while ($rowScore = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 

     $currentRanking++; 
     $score = array(); // temp user array for one group of scores 
     $numberOfCoaches; // Number of coaches with this particular number of scores 
     $scoresGroup; // Scores in the particular group 

     $score[]["scores"] = $rowScore["score"]; 
     $score[]["count"] = $rowScore["count(*)"]; 
     $numberOfCoaches[] = $score["count"]; 
     $scoresGroup[] = $score["scores"]; 

     $response[]["scoresGroup"] = $scoresGroup; // HERE IS THE PROBLEM 
+0

感謝您的回覆。所有的答案都是正確的+1我接受了最後一個。 –

2

看着你的問題的描述,你需要定義一個多維數組存儲所有從查詢結果集的結果。

請參考下面的代碼片斷

  $groupsOfScoresQuery = "SELECT score, count(*) FROM Coaches group by score ORDER BY score DESC"; 

     $result = mysqli_query($dbc, $groupsOfScoresQuery); 

     if ($result) { // query did successfully run 
      $response['topCoaches'] = array(); 

      if (mysqli_num_rows($result) > 0) { 
      while ($rowScore = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 

      $currentRanking++; 
      $score = array(); // temp user array for one group of scores 
      $numberOfCoaches; // Number of coaches with this particular number of scores 
      $scoresGroup; // Scores in the particular group 

     $score["scores"] = $rowScore["score"]; 
     $score["count"] = $rowScore["count(*)"]; 
     $numberOfCoaches = $score["count"]; 
     $scoresGroup = $score["scores"]; 

     $response["scoresGroup"][] = $scoresGroup; //Notice the array here 

     . 
     . 
     . 
     more processing 
     } // end WHILE 
+0

感謝您的回覆。所有的答案都是正確的+1我接受了最後一個。 –