Jump to content

array not showing real 'reset' value inside while statement


unistake
Go to solution Solved by requinix,

Recommended Posts

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

Link to comment
Share on other sites

  • Solution

<?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.

Link to comment
Share on other sites

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'];
		}
Link to comment
Share on other sites

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.
  • Like 1
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.