简体   繁体   中英

Can't figure how to get data from MySql Query

I asked a question earlier on how to do this query and received a great response. Unfortunately it is a bit more advanced than the queries I normally run so I cannot figure out how to retrieve the results and display them on my web page. Here is the link to the original question original question

     <?php

// Associative array
$result = $db->query("SELECT Name,
       COALESCE(AVG(CASE WHEN mth = 1 THEN PTS END), 0) AS Jan,
       COALESCE(AVG(CASE WHEN mth = 2 THEN PTS END), 0) AS Feb,
       COALESCE(AVG(CASE WHEN mth = 3 THEN PTS END), 0) AS Mar,
       COALESCE(AVG(CASE WHEN mth = 4 THEN PTS END), 0) AS Apr,
       COALESCE(AVG(CASE WHEN mth = 5 THEN PTS END), 0) AS May,
       COALESCE(AVG(CASE WHEN mth = 6 THEN PTS END), 0) AS Jun,
       COALESCE(AVG(CASE WHEN mth = 7 THEN PTS END), 0) AS Jul,
       COALESCE(AVG(CASE WHEN mth = 8 THEN PTS END), 0) AS Aug,
       COALESCE(AVG(CASE WHEN mth = 9 THEN PTS END), 0) AS Sep,
       COALESCE(AVG(CASE WHEN mth = 10 THEN PTS END), 0) AS Oct,
       COALESCE(AVG(CASE WHEN mth = 11 THEN PTS END), 0) AS Nov,
       COALESCE(AVG(CASE WHEN mth = 12 THEN PTS END), 0) AS Dec,
       AVG(PTS) AS AVG
FROM (
  SELECT Name, `Points Pass` AS PTS, MONTH(STR_TO_DATE(`OS Date`, '%a %b %e %H:%i:%s %Y')) AS mth
  FROM data
) d
GROUP BY Name");


while($row = mysqli_fetch_array($result)) {

            ?>

         <tr>
                <td><?php echo $row['Name']; ?></td>
                <td class = "gScore-<?php echo $row['Jan']; ?>"><?php echo $row['Jan']; ?></td>
                <td class = "gScore-<?php echo $row['Feb']; ?>"><?php echo $row['Feb']; ?></td>
                <td class = "gScore-<?php echo $row['Mar']; ?>"><?php echo $row['Mar']; ?></td>
                <td class = "gScore-<?php echo $row['Apr']; ?>"><?php echo $row['Apr']; ?></td>
                <td class = "gScore-<?php echo $row['May']; ?>"><?php echo $row['May']; ?></td>
                <td class = "gScore-<?php echo $row['Jun']; ?>"><?php echo $row['Jun']; ?></td>
                <td class = "gScore-<?php echo $row['Jul']; ?>"><?php echo $row['Jul']; ?></td>
                <td class = "gScore-<?php echo $row['Aug']; ?>"><?php echo $row['Aug']; ?></td>
                <td class = "gScore-<?php echo $row['Sep']; ?>"><?php echo $row['Sep']; ?></td>
                <td class = "gScore-<?php echo $row['Oct']; ?>"><?php echo $row['Oct']; ?></td>
                <td class = "gScore-<?php echo $row['Nov']; ?>"><?php echo $row['Nov']; ?></td>
                <td class = "gScore-<?php echo $row['Dec']; ?>"><?php echo $row['Dec']; ?></td>
                <td><?php echo $row['AVG']; ?></td>
            </tr>
        <?php   


}
if (!$result) { echo $db->error; }

You have a few issues:

  1. The indexes to $row need to be enclosed in quotes eg $row['name'] otherwise you will get a lot of "Undefined constant" warnings;
  2. $row['Jan'] is the correct way to retrieve the monthly data, you need to replicate that for each of the other months;
  3. replace $avgGrade with $row['AVG']
  4. I'm not sure what you're trying to achieve with
<td class = "gScore-<?php echo $row[Jan]; ?>"></td>

do you actually want

<td class = "gScore"><?php echo $row[Jan]; ?></td>
  1. you need to add the -- repeat for May to November rows to the query ie
    $result = $db->query("SELECT name,
       COALESCE(AVG(CASE WHEN mth = 1 THEN PTS END), 0) AS Jan,
       COALESCE(AVG(CASE WHEN mth = 2 THEN PTS END), 0) AS Feb,
       COALESCE(AVG(CASE WHEN mth = 3 THEN PTS END), 0) AS Mar,
       COALESCE(AVG(CASE WHEN mth = 4 THEN PTS END), 0) AS Apr,
       COALESCE(AVG(CASE WHEN mth = 5 THEN PTS END), 0) AS May,
       COALESCE(AVG(CASE WHEN mth = 6 THEN PTS END), 0) AS Jun,
       COALESCE(AVG(CASE WHEN mth = 7 THEN PTS END), 0) AS Jul,
       COALESCE(AVG(CASE WHEN mth = 8 THEN PTS END), 0) AS Aug,
       COALESCE(AVG(CASE WHEN mth = 9 THEN PTS END), 0) AS Sep,
       COALESCE(AVG(CASE WHEN mth = 10 THEN PTS END), 0) AS Oct,
       COALESCE(AVG(CASE WHEN mth = 11 THEN PTS END), 0) AS Nov,
       COALESCE(AVG(CASE WHEN mth = 12 THEN PTS END), 0) AS `Dec`,
       AVG(PTS) AS AVG
   FROM (
   SELECT name, PTS AS PTS, MONTH(STR_TO_DATE(DATE, '%a %b %e %H:%i:%s %Y')) AS mth
   FROM data
    ) d
    GROUP BY name");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM