简体   繁体   中英

PHP MySQL Loop/Foreach

I have researched for quite a while on how to get this done through other questions, but I cannot quite get a hold of it. This link seemed most similar, but I could not implement it correctly, perhaps it can help someone on this question? MySQL while loop query inside other while loop query

I am looking to display a table that uses info from our DB to display a user's parlays, games within parlays and associated wagers and odds. I would like it to look like the following:

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 can easily create this table showing all columns for each item, but the problem is, each item then becomes its own row. As you can see in my example, there is a parlay showing per row...that means there is only one parlay id, one wager, one user, one odds, etc per PARLAY. The only multiple event is the number of games per parlay that can vary between 1 or greater.

I created this table using a while loop to loop through each parlay, but unfortunately, this table shows information on each row for each column, rather than what I show in my picture.

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

        $output = '<tr>';
    $output .= '<td>'.$row['Parlay ID'].'</td>';
    $output .= '<td>'.$row['Home Team'].'</td>';
    $output .= '<td>'.$row['Away Team'].'</td>';
    $output .= '<td>'.$row['User'].'</td>';
    $output .= '<td>'.$row['Wager'].'</td>';
    $output .= '<td>'.$row['Odds'].'</td>';
    $output = '</tr>';
}

I believe that I will have to create some secondary loop inside the 'Home Team' and 'Away Team' $output's...does anyone know how I can get this to work where the loop will show all games in my PARLAYGAMES table that match the Parlay ID given by each row, in a way that can show it like my example table above?

I am thinking something similar to below. I know the format is terrible(!), but I am not sure how to write it correctly so I am trying to convey my idea/logic.

$output .= '<td>'while(*PARLAY TABLE id = $row['PARLAYGAMES.parlayid']) { echo PARLAYGAMES.gameid }'</td>';

For reference, my DB table has games listed as with the home and away teams a reference to the id of the teams table.

GAMES
id    Home Team    Away Team
1     10           5

I then have a

PARLAY TABLE
id    userid    wager    odds
1     44        1000     2.0

Finally, I have a

PARLAYGAMES TABLE
id    parlayid    gameid
1     1           1
2     1           4

Thanks for any assistance!

EDIT

I worked to keep my example as simple as possible by not showing all table columns, etc so we can get the logic/code part down, but I am going to show my whole query as it seems to make the most sense to do so:

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

Just loop through the results emitting one row per per record, watch the parlay id each time it is different from the row above emit the dividing line (eg use a border-top style on all the <td> elements)

The query looks slightly wrong. I think you should have LEFT OUTER JOIN for users and bonuses

the loop part goes like this:

$last_parlay_id=NULL;
while($row = $result->fetch_array()) {
    if($last_parlay_id != NULL && $last_parlay_id != $row['Parlay ID'] )
    {
        $output = '<tr class="has-border-top">';
        $output .= '<td>'.$row['Parlay ID'].'</td>';
    }      
    else
    {
        $output = '<tr>';
        $output .= '<td></td>';
    }

    $output .= '<td>'.$row['Home Team'].'</td>';
    $output .= '<td>'.$row['Away Team'].'</td>';
    $output .= '<td>'.$row['User'].'</td>';
    $output .= '<td>'.$row['Wager'].'</td>';
    $output .= '<td>'.$row['Odds'].'</td>';
    $output .= '</tr>';
    echo $output;
    $last_parlay_id = $row['Parlay ID'];
}

and in your CSS

   tr.has-border-top>td {
        border-top: 1px solid black
   }

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