Reputation: 11
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
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?
EDIT Thought I was making progress but now getting all roles under each system: -
$posname = mysqli_fetch_array($posresult);
echo $posname['pos_name']. '<br/>';
echo 'A total of ' .$result->num_rows. ' rows were returned<br/><br/>';
$numResults = $result->num_rows;
$all_results = array();
while ($row = mysqli_fetch_assoc($result)) {
// Append all rows to an array
$all_results[] = $row;
}
$j = 0;
while ($j < $numResults) {
$sysName = $all_results[$j]["sys_name"];
echo $sysName;
echo '<br/>';
for($i = 0; $i < $numResults; $i++){
if ($all_results[$i]["sys_name"] = $all_results[$j]["sys_name"]){
echo $all_results[$i]["role_name"];
echo '<br/>';
}
}
echo '<br/><hr/>';
$j++;
EDIT: var output. System is the same in each row, which is strange because it isn't if you run the query in MySQL?
array
(0 =>array
('rol_id' => '2','pos_name' => 'Manager A','role_name' => 'Role A','sys_name' => 'System A',),
1 =>array
('rol_id' => '1','pos_name' => 'Manager A','role_name' => 'Role B','sys_name' => 'System A',),
2 =>array
('rol_id' => '105','pos_name' => 'Manager A','role_name' => 'Role A','sys_name' => 'System A',),
3 =>array
('rol_id' => '106','pos_name' => 'Manager A','role_name' => 'Role B','sys_name' => 'System A',),
4 =>array
('rol_id' => '107','pos_name' => 'Manager A','role_name' => 'Role C','sys_name' => 'System A',),
EDIT 2: IF I grab var_export after setting the array, the systems are correct. The above is if I var_export after the loops at the bottom of the code
EDIT 3: So I was using = instead of == in my IF statement! I'm not quite there, but the output is looking a lot better. Will update when I work the last part out. Thanks for the comment below
EDIT FINAL: The below gives what I want, I now see what @ADyson was saying below about comparing the last row with the current. I've used $control for this. Had to play around with where to increment it, but it works now. Thanks all
$all_results = array();
while ($row = mysqli_fetch_assoc($result)) {
// Append all rows to an array
$all_results[] = $row;
}
//var_export($all_results);
$j = 0;
$control = $j;
//$all_results[$row['sys_name']][] = $row;
while ($j < $numResults) {
if ($control == $j){
echo 'System Name: ' . $all_results[$j]["sys_name"];
echo '<br/>';
for($i = 0; $i < $numResults; $i++){
if ($all_results[$i]["sys_name"] == $all_results[$j]["sys_name"]){
echo 'Role: ' . $all_results[$i]["role_name"];
echo '<br/>';
$control++;
}
}
echo '<br/>';
}
$j++;
}
Upvotes: 0
Views: 60
Reputation: 11
$all_results = array();
while ($row = mysqli_fetch_assoc($result)) {
// Append all rows to an array
$all_results[] = $row;
}
//var_export($all_results);
$j = 0;
$control = $j;
//$all_results[$row['sys_name']][] = $row;
while ($j < $numResults) {
if ($control == $j){
echo 'System Name: ' . $all_results[$j]["sys_name"];
echo '<br/>';
for($i = 0; $i < $numResults; $i++){
if ($all_results[$i]["sys_name"] == $all_results[$j]["sys_name"]){
echo 'Role: ' . $all_results[$i]["role_name"];
echo '<br/>';
$control++;
}
}
echo '<br/>';
}
$j++;
}
Thanks to @ADyson, see first comment for the great guidance. The $control is how I implemented the suggestion
Upvotes: 1