Grouping MySQL output using data in one of the columns Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do you set a default value for a MySQL Datetime column?Which MySQL data type to use for storing boolean valuesHow to output MySQL query results in CSV format?How do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsRetrieving the last record in each group - MySQLDuplicating a MySQL table, indices, and dataHow to reset AUTO_INCREMENT in MySQL?How to import an SQL file using the command line in MySQL?

Why are vacuum tubes still used in amateur radios?

How can I set the aperture on my DSLR when it's attached to a telescope instead of a lens?

How to compare two different files line by line in unix?

In musical terms, what properties are varied by the human voice to produce different words / syllables?

What is best way to wire a ceiling receptacle in this situation?

Time evolution of a Gaussian wave packet, why convert to k-space?

How can I prevent/balance waiting and turtling as a response to cooldown mechanics

Trademark violation for app?

Should a wizard buy fine inks every time he want to copy spells into his spellbook?

Crossing US/Canada Border for less than 24 hours

Is it possible for SQL statements to execute concurrently within a single session in SQL Server?

How fail-safe is nr as stop bytes?

Conditions when a permutation matrix is symmetric

Semigroups with no morphisms between them

Lagrange four-squares theorem --- deterministic complexity

Do wooden building fires get hotter than 600°C?

Google .dev domain strangely redirects to https

How did Fremen produce and carry enough thumpers to use Sandworms as de facto Ubers?

The Nth Gryphon Number

The test team as an enemy of development? And how can this be avoided?

Co-worker has annoying ringtone

Did any compiler fully use 80-bit floating point?

How much damage would a cupful of neutron star matter do to the Earth?

Why weren't discrete x86 CPUs ever used in game hardware?



Grouping MySQL output using data in one of the columns



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do you set a default value for a MySQL Datetime column?Which MySQL data type to use for storing boolean valuesHow to output MySQL query results in CSV format?How do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsRetrieving the last record in each group - MySQLDuplicating a MySQL table, indices, and dataHow to reset AUTO_INCREMENT in MySQL?How to import an SQL file using the command line in MySQL?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















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?



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++;










share|improve this question
























  • the secret to this is, during your loop you must keep a record of what the system name was in the previous row. If the system name in the current row is different to what it is in the current row, then you need to write a new header for the system name. If not, then don't.

    – ADyson
    Mar 22 at 14:14












  • if you're still stuck after giving that a try, provide me with the output of var_export($all_results); (so I can use it directly for testing) and I'll write you a little demo.

    – ADyson
    Mar 22 at 14:21











  • Thanks @ADyson I was just echoing out $i and $j to the screen. Even though both increment for each element in the array, and the Role Names cycle through, the System Name is always System A, even though $j is being incremented. I will edit the post now with the var output

    – markrawcliffe
    Mar 22 at 14:41











  • Seen your edits. If you do figure it out finally, please post it as an answer below (don't update your question with the answer :-)). If you still need a bit more help then by all means please edit the question further and tag me in a comment.

    – ADyson
    Mar 22 at 15:03






  • 1





    @ADyson I have just added the solution to my question sorry! Then read your comment. Will paste in the Answer now. Thanks, you solved this with your first comment

    – markrawcliffe
    Mar 22 at 15:20

















0















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?



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++;










share|improve this question
























  • the secret to this is, during your loop you must keep a record of what the system name was in the previous row. If the system name in the current row is different to what it is in the current row, then you need to write a new header for the system name. If not, then don't.

    – ADyson
    Mar 22 at 14:14












  • if you're still stuck after giving that a try, provide me with the output of var_export($all_results); (so I can use it directly for testing) and I'll write you a little demo.

    – ADyson
    Mar 22 at 14:21











  • Thanks @ADyson I was just echoing out $i and $j to the screen. Even though both increment for each element in the array, and the Role Names cycle through, the System Name is always System A, even though $j is being incremented. I will edit the post now with the var output

    – markrawcliffe
    Mar 22 at 14:41











  • Seen your edits. If you do figure it out finally, please post it as an answer below (don't update your question with the answer :-)). If you still need a bit more help then by all means please edit the question further and tag me in a comment.

    – ADyson
    Mar 22 at 15:03






  • 1





    @ADyson I have just added the solution to my question sorry! Then read your comment. Will paste in the Answer now. Thanks, you solved this with your first comment

    – markrawcliffe
    Mar 22 at 15:20













0












0








0








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?



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++;










share|improve this question
















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?



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++;







php mysql for-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 15:17







markrawcliffe

















asked Mar 22 at 11:01









markrawcliffemarkrawcliffe

112




112












  • the secret to this is, during your loop you must keep a record of what the system name was in the previous row. If the system name in the current row is different to what it is in the current row, then you need to write a new header for the system name. If not, then don't.

    – ADyson
    Mar 22 at 14:14












  • if you're still stuck after giving that a try, provide me with the output of var_export($all_results); (so I can use it directly for testing) and I'll write you a little demo.

    – ADyson
    Mar 22 at 14:21











  • Thanks @ADyson I was just echoing out $i and $j to the screen. Even though both increment for each element in the array, and the Role Names cycle through, the System Name is always System A, even though $j is being incremented. I will edit the post now with the var output

    – markrawcliffe
    Mar 22 at 14:41











  • Seen your edits. If you do figure it out finally, please post it as an answer below (don't update your question with the answer :-)). If you still need a bit more help then by all means please edit the question further and tag me in a comment.

    – ADyson
    Mar 22 at 15:03






  • 1





    @ADyson I have just added the solution to my question sorry! Then read your comment. Will paste in the Answer now. Thanks, you solved this with your first comment

    – markrawcliffe
    Mar 22 at 15:20

















  • the secret to this is, during your loop you must keep a record of what the system name was in the previous row. If the system name in the current row is different to what it is in the current row, then you need to write a new header for the system name. If not, then don't.

    – ADyson
    Mar 22 at 14:14












  • if you're still stuck after giving that a try, provide me with the output of var_export($all_results); (so I can use it directly for testing) and I'll write you a little demo.

    – ADyson
    Mar 22 at 14:21











  • Thanks @ADyson I was just echoing out $i and $j to the screen. Even though both increment for each element in the array, and the Role Names cycle through, the System Name is always System A, even though $j is being incremented. I will edit the post now with the var output

    – markrawcliffe
    Mar 22 at 14:41











  • Seen your edits. If you do figure it out finally, please post it as an answer below (don't update your question with the answer :-)). If you still need a bit more help then by all means please edit the question further and tag me in a comment.

    – ADyson
    Mar 22 at 15:03






  • 1





    @ADyson I have just added the solution to my question sorry! Then read your comment. Will paste in the Answer now. Thanks, you solved this with your first comment

    – markrawcliffe
    Mar 22 at 15:20
















the secret to this is, during your loop you must keep a record of what the system name was in the previous row. If the system name in the current row is different to what it is in the current row, then you need to write a new header for the system name. If not, then don't.

– ADyson
Mar 22 at 14:14






the secret to this is, during your loop you must keep a record of what the system name was in the previous row. If the system name in the current row is different to what it is in the current row, then you need to write a new header for the system name. If not, then don't.

– ADyson
Mar 22 at 14:14














if you're still stuck after giving that a try, provide me with the output of var_export($all_results); (so I can use it directly for testing) and I'll write you a little demo.

– ADyson
Mar 22 at 14:21





if you're still stuck after giving that a try, provide me with the output of var_export($all_results); (so I can use it directly for testing) and I'll write you a little demo.

– ADyson
Mar 22 at 14:21













Thanks @ADyson I was just echoing out $i and $j to the screen. Even though both increment for each element in the array, and the Role Names cycle through, the System Name is always System A, even though $j is being incremented. I will edit the post now with the var output

– markrawcliffe
Mar 22 at 14:41





Thanks @ADyson I was just echoing out $i and $j to the screen. Even though both increment for each element in the array, and the Role Names cycle through, the System Name is always System A, even though $j is being incremented. I will edit the post now with the var output

– markrawcliffe
Mar 22 at 14:41













Seen your edits. If you do figure it out finally, please post it as an answer below (don't update your question with the answer :-)). If you still need a bit more help then by all means please edit the question further and tag me in a comment.

– ADyson
Mar 22 at 15:03





Seen your edits. If you do figure it out finally, please post it as an answer below (don't update your question with the answer :-)). If you still need a bit more help then by all means please edit the question further and tag me in a comment.

– ADyson
Mar 22 at 15:03




1




1





@ADyson I have just added the solution to my question sorry! Then read your comment. Will paste in the Answer now. Thanks, you solved this with your first comment

– markrawcliffe
Mar 22 at 15:20





@ADyson I have just added the solution to my question sorry! Then read your comment. Will paste in the Answer now. Thanks, you solved this with your first comment

– markrawcliffe
Mar 22 at 15:20












1 Answer
1






active

oldest

votes


















1














$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






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55298201%2fgrouping-mysql-output-using-data-in-one-of-the-columns%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    $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






    share|improve this answer



























      1














      $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






      share|improve this answer

























        1












        1








        1







        $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






        share|improve this answer













        $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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 22 at 15:22









        markrawcliffemarkrawcliffe

        112




        112





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55298201%2fgrouping-mysql-output-using-data-in-one-of-the-columns%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript