1

I am looking to echo values from two tables which are related on a HTML page. To do this I have queried the tables and set a while loop within a while loop to echo the output.

The database tables each have a column of a unique_id which is a foreign key in the next table. The first table is queried using the session email of the user to get that unique_id. The next table is then queried using that value.

The first set of data echo's fine but the next is blank.

Code:

    <!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>

And the current output:enter image description here

2
  • <?php if($hole_row['course_id'] === $row['unique_id']){ ?> This is stopping the rest of the records from coming through. Try omitting it Commented May 15, 2017 at 18:02
  • same result I'm afraid Commented May 15, 2017 at 18:03

1 Answer 1

1

You need to do your nested query inside your outer loop and since you are already setting $row = mysqli_fetch_assoc($rnd_id_query) change to this:

$round_id = $row['unique_id'];
$hole = mysqli_query($connect, "SELECT * FROM holes WHERE course_id = '$round_id'");
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Working like a charm. Will accept answer shortly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.