I am having difficulty in outputting the correct information from my SELECT query. These are tables I am pulling from.
I have a list of games:
GAMES
id home_team away_team
1 1 2
2 3 4
3 12 16
Where these home_team and away_team id's match up to:
TEAMS
id name
1 Team A
2 Team B
3 Team C
Additionally, I have a parlays table and parlay games. Parlay Games references parlays table.
PARLAYS
id userid bettingsiteid wager odds
1 44 1 1000 2.0
2 45 5 1500 2.3
3 12 14 2000 1.8
PARLAY GAMES
id parlayid gameid betinfo
1 1 1 Home Win
2 1 2 Away Win
3 1 5 Home Win
4 2 18 Home Win by 2 goals
This is what I would like to see in my final table:
Parlay ID Home Team Away Team User Wager Odds
Team A Team B
1 Team C name of teams.id = 4 Ridge Robinson 1000 2.0
name of teams.id = 12 name of teams.id = 16
-------------------------------------------------------------- (<tr> border)
Team C name of teams.id = 4
2 name of teams.id = x name of teams.id = x
-------------------------------------------------------------- (<tr> border)
I hope that makes sense. I just want the parlay id and then the name of each home and away team in the parlay.
This is my select query I am currently trying:
SELECT
u.first_name AS 'User First Name',
u.last_name AS 'User Last Name',
u.id AS 'User ID',
ht.name AS 'Home Team',
away.name AS 'Away Team',
p.wager AS 'Wager',
p.odds AS 'Odds',
pg.parlayid AS 'Parlay ID',
pg.betinfo AS 'Bet Info',
pg.gameid AS 'Game ID',
g.date AS 'Game Date',
b.betting_site_name AS 'Betting Site'
FROM parlays p
JOIN parlaygames pg ON p.id = pg.parlayid
JOIN games g ON pg.gameid = g.id
JOIN teams ht ON g.home_team = ht.id
JOIN teams away ON g.away_team = away.id
JOIN users u ON u.id = p.userid
JOIN bonuses b ON p.bettingsiteid = b.id
ORDER BY
pg.parlayid,
pg.gameid
And this is my table code:
<table class="table table-striped">
<tr>
<th>User Name</th>
<th>Date</th>
<th>Betting Site</th>
<th>Home Team</th>
<th>Away Team</th>
<th>Wager</th>
<th>Odds</th>
<th>Manager</th>
<th>Bet Info</th>
<th>Success</th>
<th>Edit</th>
</tr>
<?php
while($row = $result->fetch_array()) {
$hometeams = implode(' ',$row['Home Team']);
$output = '<tr>';
$output .= '<td><a href="userprofile.php?id='.$row['User ID'].'">'.$row['User First Name'].' '.$row['User Last Name'].'</a></td>';
$output .= '<td>'.$row['Game Date'].'</td>';
$output .= '<td>'.$row['Betting Site'].'</td>';
$output .= '<td>'.$hometeams.'</td>';
$output .= '<td>'.$row['Away Team'].'</td>';
$output .= '<td>'.$row['Wager'].' kr</td>';
$output .= '<td>'.$row['Odds'].'</td>';
$output .= '<td><a href="managerprofile.php?id='.$row['Manager ID'].'">'.$row['Manager First Name'].' '.$row['Manager Last Name'].'</a></td>';
$output .= '<td>'.$row['Bet Info'].'</td>';
$output .= '<td>'.$row['Success'].'</td>';
$output .= '<td><a href="edit_userbet.php?id='.$row['User ID'].'&betsite='.$row['Betting ID'].'&gameid='.$row['Game ID'].'&wager='.$row['Wager'].'&odds='.$row['Odds'].'&betinfo='.$row['Bet Info'].'" class="btn btn-default btn-sm">Edit</a></td>';
$output .= '</tr>';
echo $output;
}
?>
</table>
But this is very wrong as I am getting a table that shows (dumbed down)
Parlay ID Home Team Away Team Wager Odds
1 1 1 1000 2.0
1 2 2 1000 2.0
1 3 3 1000 2.0
-------------------------------------------------------------- (<tr> border)
I know this is because I inserted $gameid, but I am unsure how to get the home and away team names per game id without referencing $gameid per row.
Does anybody have any suggestions?
EDIT One of the reasons that I am organizing the table like this is because, in my parlays table, in addition to just the parlay id and home team/away team...I also have one wager and one odds listed for each set. Perhaps someone has a better idea of a foreign key relationship that could work better and solve this problem that way?
**EDIT #2 ** I have changed my DB table structure, but still need help displaying my front end table correctly!