简体   繁体   中英

Trying to get all data from one table

I am trying to get all data from a table and output it using ids from another. I have teams which have members and each member has aa set of scores.

I need to output the members and and there scores by using my thirs table which holds the id of the member and team so a team_members table.

Each member has 8 scores which is a single value or integer from 1-5, no more and no less. I want to simply output the members in rows which there scores next to them. I am really struggling:

I am storing the team id in a sessions from a form input so my select query can look at that ID to show me members from that specific teams:

$sql = "SELECT t.team_name AS team_name, m.score_1, GROUP_CONCAT(m.firstName, ' ', m.lastName) AS team_members 
FROM members AS m
JOIN team_members AS tm
ON tm.member_id = m.member_id 
JOIN teams AS t
ON t.team_id = tm.team_id
WHERE t.dashboard_id = $dashboard_id AND t.team_id = $teamSelect
GROUP BY t.team_name"; 

if(!$result = $conn->query($sql)) {}

while($row = $result->fetch_assoc()){

echo '<h2>' . $row["team_name"] . '</h2><br>';

$names = explode(',', $row['team_members']);

echo '<div class="teamTable">';

echo '<div class="tableHeader">';
echo '<div class="col">&nbsp;</div>';
echo '<div class="col">SDO</div>';
echo '<div class="col">DCTO</div>';
echo '<div class="col">ED</div>';
echo '<div class="col">CA</div>';
echo '<div class="col">DHPT</div>';
echo '<div class="col">IRT</div>';
echo '<div class="col">GL</div>';
echo '<div class="col">IL</div>';
echo '</div>';
} 

What i am than doing at the moment is outputting the members names in rows by team id

foreach($names as $name) {
$namearray = preg_split('/\s+/', $name);
echo '<div class="tableNameRow">';
echo '<div class="col">' . $name . '</div>';
$sql2 = "SELECT score_1 FROM members WHERE firstName + lastName = '" . $name . "'";
$result2 = $conn->query($sql2);
if($result2->num_rows > 0){
while($row2 = $result2->fetch_assoc()){
echo '<div class="col">' . $row2['score_1'] . '</div>';
}
echo '<br>';
}
echo '</div>';
}

All I want to do is show each member in rows which their 8 scores next to them like so:

member1 1 2 3 4 5 2 3 4
member2 4 3 2 5 1 2 4 2
member3 2 3 4 5 5 4 3 1

I have only included score_1 in my SELECT to grab that sore to begin with. What happens in my code above is it outputs each user by row but its then takes score_1 from every member and and puts it on the same row as the members name.

TABLES

members
member_id(PK), firstName, lastName, score_1, score_2, score_3, score_4, score_5, score_6, score_7, score_8 

teams
team_id(PK), team_name

team_members
team_member_id, member_id(FK), team_id(FK)

For anyone that wants to know i figured it out by changing the select:

$dansql2 = "SELECT team_members.team_id, team_members.member_id, members.member_id, members.firstName, members.lastName, members.score_1, members.score_2, members.score_3, members.score_4, members.score_5, members.score_6, members.score_7, members.score_8
            FROM team_members 
            JOIN members
            ON team_members.member_id = members.member_id
            WHERE members.dashboard_id = $dashboard_id AND team_members.team_id = $teamSelect
            ORDER BY members.firstName ASC";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM