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);
}