php


PHP echoing from within a nested while loop


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:
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'");

Related Links

php get_file_contents failing
htmlentities works on browser only when called twice [closed]
Construct a class and run methods from command line in php
One table with many field or two table with fewer fields?
Mysql database connection file errors
simple === matching failed php
Laravel controller return to route
eloquent query that finds top 5 records
make in order the most repeated words in a text file
How to manage unsubscribe link in a newsletter with mailjet PHP API?
Search between 2 dates with different format
How to pass another parameter in limit of cakephp 3.2
Yii2 : view file and wbraganca dynamic forms
How to close Mysql connection [duplicate]
PHP zip file download returns empty zip initially, works subsequently
PHP7 gives error "PHP Startup: Unable to load dynamic library '/usr/lib64 /php/modules/mysqli.so'

Categories

HOME
android-espresso
long-integer
antd
vsts-build
joomla3.2
bellman-ford
stack-overflow
xamarin-zebble
fireloop
responsivevoice
jquery-select2-4
pushdown-automaton
django-rq
nest-api
vertica
adminlte
jest
azure-machine-learning
android-permissions
workday
perfino
pdf.js
function-points
acrobat
sfml
xenapp
instructions
vcenter
caesar-cipher
appcode
microsoft-certifications
gsp
counting
express-session
netstat
variable-variables
django-smart-selects
sspi
assert
suds
fiware-wirecloud
notesview
jqgrid-asp.net
transparency
rightnow-crm
entropy
webvtt
sequence-diagram
pen
popen
1010
openerp-6
nrf51
finder
jsf-2.3
system.net.mail
openseadragon
maximize
simian
sharepoint-apps
teamviewer
flush
lovefield
artisan
clarion
shoes
debug-symbols
createfile
fat32
hp-idol-ondemand
string.format
box2dweb
coding-efficiency
stripe.net
rails-api
meteor-collections
wso2bam
oxygene
flipboard
typesafe-stack
lsa
unrealscript
email-verification
ip-geolocation
workflow-manager-1.x
border-box
cakeyframeanimation
infopath-2007
radscheduler
getopts
kernel32
turbine
eaccelerator
jdownloader
source-code-protection
efs

Resources

Encrypt Message