0

I have this table in MySql db:

enter image description here

After running this query:

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

The result table look like this:

enter image description here

Now in php I try to fetch the result and iterate through the array to determine which group each coach belongs to and get his place in the ranking. Therefore I wrote this:

$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

Why $response["scoresGroup"] will always conatins the last value from the result? In this case this is 123. I thought that this is the first iteration through the loop and $response["scoresGroup"] wll hold first element (474), during the second iteration should hold 382 ? What I'm doing wrong here? Do I use correct function to fetch result? or should I use different loop to acheive my goal? Thanks for the help in advance.

4 Answers 4

2

You did not post the expected structure of $response; here is what I think you are trying to do:

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

Or perhaps:

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
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
1
  • Thanks for the reply. All the answers are correct +1 I have accepted the last one. Commented Nov 1, 2012 at 16:46
2

Looking at the description of your question, you need to define a multidimensional array for storing all the results from query resultset.

Please refer the below code snippet

           $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
1
  • Thanks for the reply. All the answers are correct +1 I have accepted the last one. Commented Nov 1, 2012 at 16:46
1

You are settings $response['scoresGroup'] each time you run the loop, so at the end, it will contain only the last element. Try changing the variable you put the data into on each loop.

$x++;
$response['scoresGroup' . x] = $scoresGroup;
1
  • Thanks for the reply. All the answers are correct +1 I have accepted the last one. Commented Nov 1, 2012 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.