How to output a group, based on the same column cell, while looping it out into html?

Multi tool use


How to output a group, based on the same column cell, while looping it out into html?
I want to create groups while looping all my rows from a database table.
example database:
id | sex | name | age
1 | male | kevin | 20
2 | male | bob | 24
3 | female | jenny | 24
4 | female | bob | 20
5 | no | gaben | 13
I want to group the entries by names.
The output i'm looking for is something like this:
Should output:
<div id="bob">
<a>bob male 24</a>
<a>bob female 20</a>
</div>
<div id="kevin">
<a>kevin male 20</a>
</div>
<div id="gaben">
<a>gaben no 13</a>
</div>
<div id="jenny">
<a>jenny female 24</a>
</div>
I was making two while loops inside of eachother. It only outputs one <div>
for one name
. Each div should be created for each unique name
. So when someone has the same name
, they should be put together into the same <div id="name">
element/group;
<div>
name
name
name
<div id="name">
I created a names
array, so i could check if the name is inside the names array. If not, it should create a <div id="name">
element. After the <div id="name">
element/group was created, the inner loop should loop all the $name = $row['name']
inside that div element. If the loop was finished, the div should be closed </div>
and the name added into the $names = $name
array. So it wouldn't create the same div
again. Then it should continue with the next name, but it somehow doesn't do that.
names
<div id="name">
<div id="name">
$name = $row['name']
</div>
$names = $name
div
<?php
if($resultCheck > 0){
$names = ;
while($row = $result->fetch_assoc()) {
$name = $row['name'];
if(!in_array($fieldset, $fieldsets)){
echo '<div name="'.$hersteller.'">';
}
while($names = $result->fetch_assoc()){
echo '<a>'.$row['name'].' '.$row['sex'].' '.$row['age'].'</a>';
}
}
if(!in_array($name, $names)){
echo '</div>';
}
$names = $name;
}
?>
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.