PHP在嵌套的while循环中回显

时间:2017-05-15 17:55:40

标签: php html mysql

我希望回显两个与HTML页面相关的表的值。为此,我查询了表并在while循环中设置了一个while循环来回显输出。

每个数据库表都有一列unique_id,它是下一个表中的外键。使用用户的会话电子邮件查询第一个表以获取该unique_id。然后使用该值查询下一个表。

第一组数据echo很好,但下一个是空白的。

代码:

    <!DOCTYPE HTML>
    <html>
    <head>
    <title>Your Scorecards</title>
    <link rel="icon" type="image/x-icon" href="../assets/favicon-32x32.png">
    <link rel="stylesheet" href="stylesheet.css">
    </head>

    <body>
    <?php include_once 'header.php'; ?>
    <?php
    $connect =mysqli_connect('localhost','root','','micaddy');
    $id_query = mysqli_query($connect, "SELECT unique_id FROM users WHERE email = '{$_SESSION['login_user']}'");
    $id_array = mysqli_fetch_assoc($id_query);
    $uid = $id_array['unique_id'];

    $result = mysqli_query($connect, "SELECT * FROM rounds WHERE user_id = '$uid'");

    $rnd_id_query = mysqli_query($connect, "SELECT unique_id FROM rounds WHERE user_id = '$uid'");
    $rnd_id_array = mysqli_fetch_assoc($rnd_id_query);
    $round_id = $rnd_id_array['unique_id'];

    $hole = mysqli_query($connect, "SELECT * FROM holes WHERE course_id = '$round_id'");


    ?>
    <div class="container">
        <div class="row">
            <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading clearfix"><h3 class="panel-title"><strong>Your Rounds</strong></h3></div>
                <?php while($row=mysqli_fetch_assoc($result)):?>
                    <div class="panel-body">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-lg-5">
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><h3 class="panel-title"><strong><?php echo $row['course_name'];?> &nbsp <?php echo $row['round_date']?> </strong></h3></div>
                                            <div class="panel-body">
                                            <table style="width:100%">
                                                <tr>
                                                    <th>Hole Number</th>
                                                    &nbsp
                                                    <th>Yards</th>
                                                    &nbsp
                                                    <th>Par</th>
                                                    &nbsp
                                                    <th>Shots</th>
                                                    <?php while($hole_row=mysqli_fetch_assoc($hole)): ?>
                                                    <?php if($hole_row['course_id'] === $row['unique_id']){ ?>
                                                    <tr>
                                                        <td><?php echo $hole_row['hole_number']; ?></td>
                                                        <td><?php echo $hole_row['yards']; ?></td>
                                                        <td><?php echo $hole_row['par']; ?></td>
                                                        <td><?php echo $hole_row['shots']; ?></td>
                                                    </tr>
                                                    <?php } ?>          
                                                    <?php endwhile; ?>  
                                                </tr>
                                            </table>                                        
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endwhile;?>
            </div>
        </div>
    </div>
</div>
<?php include_once 'footer.php'; ?>
</body>
</html>

当前输出:enter image description here

1 个答案:

答案 0 :(得分:1)

您需要在外部循环中执行嵌套查询,因为您已经将java.lang.ClassCastException: maakesher.com.black.maakesher.psychoQuizzesActivity$1$1 cannot be cast to android.content.res.XmlBlock$Parser 更改为此:

$row = mysqli_fetch_assoc($rnd_id_query)