Query result:
+--------+-----------+--------------+-----------+
| rol_id | pos_name | role_name | sys_name |
+--------+-----------+--------------+-----------+
| 2 | A Manager | Role A | System A |
| 1 | A Manager | Role B | System A |
| 105 | A Manager | Role A | System B |
| 106 | A Manager | Role B | System B |
| 107 | A Manager | Role C | System B |
| 108 | A Manager | Role D | System B |
| 4 | A Manager | Role A | System C |
| 25 | A Manager | Role A | System C |
| 100 | A Manager | Role A | System C |
Required output:
Position Name “A Manager” can access
- “System A”
- “Role A”
- “Role B”
- “System B”
- “Role A”
- “Role B”
- “Role C”
- “Role D”
etc.
Current code, using a for
loop to grab the system names:
$posname = mysqli_fetch_array($posresult);
echo $posname['pos_name']. '<br/>';
echo 'A total of ' .$result->num_rows. ' rows were returned<br/>';
$numResults = $result->num_rows;
$all_results = array();
while ($row = mysqli_fetch_assoc($result)) {
// Append all rows to an array
$all_results[] = $row;
}
for($i = 0; $i < $numResults; $i++){
echo $all_results[$i]["sys_name"];
echo '<br/>';
}
I was intending to learn nested loops next, to put the roles under each system.
But the above lists each system for every row in the array (expected behaviour with the current code), how could I group the output so it looks as above, with each system listed once and each role associated with it printed beneath it?