unistake Posted April 7, 2014 Share Posted April 7, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/287575-a-while-within-a-while/ Share on other sites More sharing options...
requinix Posted April 7, 2014 Share Posted April 7, 2014 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". Quote Link to comment https://forums.phpfreaks.com/topic/287575-a-while-within-a-while/#findComment-1475209 Share on other sites More sharing options...
unistake Posted April 7, 2014 Author Share Posted April 7, 2014 Thanks requinix, I wrote the outputs by hand and forgot to include the Rank as you said. The data comes out as I need it except the first output from the first while statement is coming out several times. Quote Link to comment https://forums.phpfreaks.com/topic/287575-a-while-within-a-while/#findComment-1475211 Share on other sites More sharing options...
requinix Posted April 7, 2014 Share Posted April 7, 2014 If it is doing that then the first query is returning multiple rows. Have you checked that yet? Quote Link to comment https://forums.phpfreaks.com/topic/287575-a-while-within-a-while/#findComment-1475296 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.