-1

I have a SQL table called 'incidents' which holds information on behavioural incidents in a school.

I have the following SQL statement which returns the number of occurrences of each 'type'

SELECT `type`, count(*) FROM `incidents` WHERE `studentID` = :id GROUP BY `type`

Basically, I'm looking to create a chart to show the number of occurrences of each incident type in the table 'incidents'

I var_dumped what it returns and I get this

array(2) { 
    [0]=> array(4) { ["type"]=> string(9) "Classwork" [0]=> string(9) "Classwork" ["count(*)"]=> string(1) "3" [1]=> string(1) "3" } 
    [1]=> array(4) { ["type"]=> string(8) "Bullying" [0]=> string(8) "Bullying" ["count(*)"]=> string(1) "1" [1]=> string(1) "1" } 
}

I'm really struggling to parse this to create meaningful output. How would I loop through this array and output the number of each incident type? For example, to output:

3 classwork incident
1 bullying incident

I've tried the following to test the output with a foreach but it just says I cannot offset the array by "type"

function orderByType($id){
    include_once 'includes/db_connection.php';
    $dbconn = OpenCon();
    $dbconn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $sqlstmnt2 = 'SELECT `type`, count(*) FROM `incidents` WHERE `studentID` = :studentID GROUP BY `type`';
    $stmtUsr2 = $dbconn -> prepare($sqlstmnt2);
    $intid = intval($id);
    $stmtUsr2 -> bindValue(':studentID', $intid);
    $stmtUsr2 -> execute();
    $rows = $stmtUsr2->fetchAll();
    //list found pupils
    if(is_null($rows))
            {
                //list found pupils
                return 0;
            }
    //return $rows;
    foreach($rows as $item){
        echo($item[0]["type"]);
    }
    var_dump($rows);
}

3 Answers3

0
foreach($rows as $item){
        echo($item["count(*)"]);
    }
0

I'd recommend starting by providing an alias for "count(*)". eg: cnt

SELECT `type`, count(*) as cnt FROM `incidents` WHERE `studentID` = :studentID GROUP BY `type`

Then switch your $item and $rows around:

foreach($rows as $item){
    echo($item["cnt"]);
}

Also you can try using a while loop using fetch(PDO::FETCH_ASSOC) so it only returns the values once with associative names. Use \n instead of <br> after "incident" if HTML isn't your desired output.

while ($row = $stmtUsr2->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
    echo $row['cnt'] . " " . $row['type'] . " incident<br>";
}

This way a var_dump of $row will look like this:

array(2) { 
    [0]=> array(2) { ["type"]=> string(9) "Classwork" ["cnt"]=> string(1) "3" } 
    [1]=> array(2) { ["type"]=> string(8) "Bullying" ["cnt"]=> string(1) "1" } 
}

And the loop will output as requested:

3 classwork incident
1 bullying incident
matteo
  • 71
  • 7
-1

Since $rows is an array which contains other arrays (matrix) you probably need 2 for loops. Try this:

for(int $i=0;$i < count($rows); $i++){
 for(int $j=0; $j< count($rows[i]); $j++).  {
     echo $rows[$i][$j];
   }
echo $rows[$i]["type"];
echo $rows[$i]["count*"];

}

Mattia Monari
  • 158
  • 1
  • 3
  • 9