简体   繁体   English

PHP MySQL循环/ Foreach

[英]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 MySQL在其他while循环查询内部的while循环查询

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. 正如您在我的示例中看到的那样,每行有一个显示的联结...这意味着每个联结只有一个联结id,一个赌注,一个用户,一个赔率等。 The only multiple event is the number of games per parlay that can vary between 1 or greater. 唯一的多个事件是每个连击的游戏数量可以在1或更大之间变化。

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循环创建此表,以循环遍历每个中继,但是不幸的是,该表显示了每一列每一行的信息,而不是我在图片中显示的信息。

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? 我相信我将不得不在“主队”和“客队”的$ output内创建一些辅助循环……有人知道我如何使它起作用,该循环将在我的PARLAYGAMES表中显示匹配的所有游戏每行给定的Parlay ID,可以像上面的示例表一样显示吗?

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. 作为参考,我的数据库表列出了与主队和客队的比赛,并列出了对球队表ID的引用。

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) 只需循环遍历每条记录发出一行的结果,每次观察与上一行发出分隔线不同的parlay id(例如,在所有<td>元素上使用border-top样式)

The query looks slightly wrong. 该查询看起来有些错误。 I think you should have LEFT OUTER JOIN for users and bonuses 我认为您应该有LEFT OUTER JOIN来获得usersbonuses

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 并在您的CSS中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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