In my tables here
names countries scores
[id -- FirstName -- LastName] [id -- cid -- Country] [id -- sid -- Score]
[1 -- FName1 -- LName1 ] [1 -- 1 -- USA ] [1 -- 1 -- 5]
[2 -- FName2 -- LName2 ] [2 -- 1 -- France ] [2 -- 1 -- 6]
[3 -- FName3 -- LName3 ] [3 -- 1 -- Germany] [3 -- 2 -- 7]
[4 -- FName1 -- LName1 ] [4 -- 2 -- Germany] [4 -- 2 -- 8]
[5 -- 2 -- Czech ] [5 -- 2 -- 2]
[6 -- 3 -- Germany] [6 -- 4 -- 12]
[7 -- 4 -- Czech ] [7 -- 3 -- 15]
I get the data and make it in this structer
.Container div {
border: 1px solid blue;
margin: 10px;
}
.Container div div {
border: 1px solid red;
margin: 10px;
}
<div>
<div id="Master German">German</div>
<div id="German">
<div class="Container">
<div id='FName2 German'>
<div>FName2</div>
<div>LName2</div>
</div>
<div id='FName1 German'>
<div>FName1</div>
<div>LName1</div>
</div>
<div id='FName3 German'>
<div>FName3</div>
<div>LName3</div>
</div>
</div>
</div>
<div id="Master France">France</div>
<div id="France">
<div class="Container">
<div id='FName1 France'>
<div>FName1</div>
<div>LName1</div>
</div>
</div>
</div>
<div id="Master Czech">Czech</div>
<div id="Czech">
<div class="Container">
<div id='FName4 Czech'>
<div>FName4</div>
<div>LName4</div>
</div>
<div id='FName2 Czech'>
<div>FName2</div>
<div>LName2</div>
</div>
</div>
</div>
<div id="Master USA">USA</div>
<div id="USA">
<div class="Container">
<div id='FName1 USA'>
<div>FName1</div>
<div>LName1</div>
</div>
</div>
</div>
</div>
using the following query
SELECT
a.FirstName,
a.LastName,
c.Country,
SUM(b.Scores) AS Score
FROM names a
INNER JOIN countries c
ON a.id = c.cid
LEFT JOIN scores b
ON a.id = b.sid
WHERE a.id = :id
GROUP BY a.id
ORDER BY Score ASC
Now my problem appears when for example id[1]
and id[4]
has the same FName
and LName
but the scores are different
Since i order them by Score
and id[4]
> id[1]
the structer supposed to be like
Germany:
FName2 -- LName2 [Score: 15]
FName3 -- LName3 [Score: 15]
FName1 -- LName1 [Score: 11]
Czech:
FName2 -- LName2 [Score: 15]
FName1 -- LName1 [Score: 12]
But what i get is
Germany:
FName2 -- LName2 [Score: 15]
FName3 -- LName3 [Score: 15]
Czech:
FName2 -- LName2 [Score: 15]
FName1 -- LName1 [Score: 12]
Germany:
FName1 -- LName1 [Score: 11]
I tried all the possible group by
but nothing worked there is always an extra country
div
My php code is
$pre = null;
echo "<div>";
$end = "";
while ($row = $stmt->fetch()) {
$FName = $row['FirstName'];
$LName = $row['LastName'];
$Country = $row['Country'];
if ($Country != $pre) {
echo $end;
echo "<div id='Master $Country'>$Country</div>";
echo "<div id='$Country'>";
}
$end = "</div>
</div>";
$pre = $Country;
echo "
<div id='$FName USA'>
<div>$FName</div>
<div>$LName</div>
</div>";
}
echo $end;
echo "</div>";