Jump to content

a while() within a while()


unistake

Recommended Posts

Hi guys, 

 

I am trying to create a while condition that would display an output such as: 

 

"Duty: 1234, = Dep: 01:00, End: 03:35

code1, code4, code5"

 

The problem I am having is instead of echoing the first line once, with my while statements the first line will echo as many times are there are codes associated with it that I am trying to output from another mysql table. 

 

Have a look below and you will see what I mean! My output at the moment looks like:

 

"Duty: 1234, = Dep: 01:00, End: 03:35

code1

Duty: 1234, = Dep: 01:00, End: 03:35

code4

Duty: 1234, = Dep: 01:00, End: 03:35

code5"
$sql = "SELECT * FROM calendar WHERE Duty = '$duty' AND BeginTime = '$begintime' AND EndTime = '$endtime'";
$result = mysqli_query($cxn,$sql)
	or die ("Can't execute query.");

while($row=mysqli_fetch_assoc($result)) {
	echo 'Duty: '.$row['Duty'].' = Dep:'.$row['BeginTime']. ', Land:'.$row['EndTime'].'<br />';

	$sql1 = "SELECT * FROM users WHERE Code = '{$row['Code']}'";
	$result1 = mysqli_query($cxn,$sql1)
		or die ("Can't execute query1.");

	while($row1 = mysqli_fetch_assoc($result1)) {
		echo $row1['Rank'].': '.$row['Code'].'</br>';
	}
}

Thanks!

Link to comment
Share on other sites

Have you verified that you data reflects what you think is supposed happen? It looks to me like there are three separate calendar entries, each with a different code. And your code shows a "Rank: Code" format that isn't shown in your output. Which does have stray commas in the Duty lines.

 

Ha, ha, "duty".

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.