unistake Posted December 22, 2015 Share Posted December 22, 2015 Hi all, I am trying to echo reset($starttimes) to show the first value in my $starttimes array however i it is only showing the second value in the array and skipping the first i want to echo. The statement I have written is inside a while statement however I am not sure if that is where lies the problem. Please take a look at line 29 here: http://pastebin.com/2KQ7kgy9 Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted December 22, 2015 Solution Share Posted December 22, 2015 <?php $sqlfriend = "SELECT * FROM Users INNER JOIN friends ON friends.FriendCode = Users.Code WHERE friends.Code = '{$_SESSION['Code']}' ORDER BY Users.Code DESC"; $resultfriend = mysqli_query($cxn,$sqlfriend) or die ("Cant get friends."); while($rowfriend=mysqli_fetch_assoc($resultfriend)) { // CHECKS IF WORKING, STBY or OFF $sql = "SELECT Duty,BeginTime,EndTime FROM rosters WHERE Code = '{$rowfriend['FriendCode']}' AND SectorDate = '$today' ORDER BY BeginTime ASC"; $result2 = mysqli_query($cxn,$sql) or die ("Cant find out friends duties for today."); $duty = mysqli_fetch_assoc($result2); if(strpos($duty['Duty'],'SBY') !== false) { // friend STBY $border = 'warning'; $friendsdutytoday = 'Today: <br /> <b>Standby until '.$duty['EndTime'].'</b>'; } elseif (strpos($duty['Duty'],'OFF') !== false || strpos($duty['Duty'],'A/L') !== false || strpos($duty['Duty'],'ADHOC') !== false) { // friend $border = 'success'; $friendsdutytoday = 'Today: <br /> <b>'.$duty['Duty'].'</b>'; } elseif(is_numeric($duty['Duty'])){ // friend WORKING $starttimes = array(); $finishtimes = array(); while($duty=mysqli_fetch_assoc($result2)) { $starttimes[] = $duty['BeginTime']; $finishtimes[] = $duty['EndTime']; } $border = 'info'; $friendsdutytoday = 'Working today <br />'; foreach($starttimes as $value) { echo $value; } echo '<b>'.reset($starttimes).'Z - '.end($finishtimes).'Z</b>'; /////// ERROR ECHOED HERE ///////// } else { $border = 'info'; $friendsdutytoday = 'Today <br /> <b>No Roster</b>'; }Right. Because the first row is the $duty you fetched on line 15. When you start the while loop (31) you throw away $duty and start fetching more rows. Instead of using an empty array for $starttimes and $finishtimes, use an array with the values you already have in $duty. Quote Link to comment Share on other sites More sharing options...
unistake Posted December 22, 2015 Author Share Posted December 22, 2015 Great explanation! Cheers! Finished code: elseif(is_numeric($duty['Duty'])){ // friend WORKING $starttimes = array(); $finishtimes = array(); $starttimes[] = $duty['BeginTime']; $finishtimes[] = $duty['EndTime']; while($duty=mysqli_fetch_assoc($result2)) { $starttimes[] = $duty['BeginTime']; $finishtimes[] = $duty['EndTime']; } Quote Link to comment Share on other sites More sharing options...
requinix Posted December 22, 2015 Share Posted December 22, 2015 For completeness, there's an alternative solution: changing the while loop into a do/while loop. elseif(is_numeric($duty['Duty'])){ // friend WORKING $starttimes = array(); $finishtimes = array(); do { $starttimes[] = $duty['BeginTime']; $finishtimes[] = $duty['EndTime']; } while($duty=mysqli_fetch_assoc($result2));Using this structure requires knowing that $duty starts off with a valid value (ie, the query returned at least one row). The code satisfies that requirement already because of the is_numeric() check; it's not as good as an explicit check that $duty isn't empty, and will potentially raise warnings/notices, but it works. 1 Quote Link to comment 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.