Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    424
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Am I using BREAK correclty ??

    I have a kinda hackish WHILE loop, and I'm trying to break 2; at a certain point.
    I'm not getting ANY print when I'm expecting it, and that's where my suspicion lands.

    NOTE: This is actually the next phase in a question started here:
    Quote Originally Posted by mOrloff View Post
    ... The goal here is to get a report, per day, of all the activity broken into IN/OUT pairs...
    Here's my code (probably my most complex loop to-date):
    PHP Code:
    $inOut=1;  // a toggle which will tell us whether to look for an IN (1) or an OUT (0)
    $inEmp=$outEmp=''// instantiate
    $numRows=mysql_num_rows($result);
    $i=0;
    while(
    $i<=$numRows){
        while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)){
            
    $i++;
            
    $unixTime=strtotime($row['dateTime']);  // convert to a universal format
            
    $time=date('g:i:s A'$unixTime);  // the time (in 12hr format)
            
    $empId=$row['empId'];
        
    ### look for clock-IN data ###
            
    if($inOut==1){  // we are looking for an IN time
                
    if($row['isIn']){  // if this is a Clock IN ...
                    
    $inValue='<td>'.$time.'</td>'// print the start time
                    
    $inEmp=$empsAry[$empId]["fName"].' '.$empsAry[$empId]["mi"].' '.$empsAry[$empId]["lName"];  // set the name
                    
    $start=$unixTime;  // capture it as a "START" time
                
    }else{  // we're looking for an IN, but got an OUT
                    
    $inValue='<td></td>';  // define as blank
                    
    $inEmp='';
                }  
    // end IF/ELSE($row['isIn'...
                
    $inOut--;  // flip the toggle so that we look for an OUT next.
            
    }  // end IF($inout==='in')...
        ### look for clock-OUT data ###
            
    if($inOut==0){  // we are looking for an OUT time
                
    if(!$row['isIn']){  // if this is a Clock OUT ...
                    
    $outValue='<td>'.$time.'</td>';  // print the time
                    
    $outEmp=$empsAry[$empId]["fName"].' '.$empsAry[$empId]["mi"].' '.$empsAry[$empId]["lName"];  // set the name
                    
    $stop=$unixTime;  // capture it as a "STOP" time
                
    }else{  // we're looking for an OUT, but got an IN
                    
    $outValue='<td></td>';  // define as blank
                    
    $outEmp='';
                }  
    // end IF/ELSE(!$row['isIn'...                        
                
    $inOut++;  // flip the toggle so that we look for an IN next.
                
    break 2;  // anytime we finish the OUT portion, we want to jump out and assemble what we've gathered
            
    }  // end IF($inout==='out')...
        
    }  // end WHILE($row...    
    ### put it all together ###
        
    echo 'TEST. We have broken out succefully <br/>';  // for testing
        
    if($inEmp==$outEmp){
            echo 
    'TEST. In and Out emps ARE the same <br/>';  // for testing
            
    $buffer.='<tr>'."\n";  // start a new row
            
    $buffer.='<td>'.$inValue.'</td>'// include the seporator column
            
    $buffer.='<td>-</td>'// include the seporator column
            
    $buffer.='<td>'.$outValue.'</td>'// include the seporator column
            
    $buffer.='<td>'.$inEmp.'</td>'// emp name
            
    $buffer.='</tr>'."\n";  // end the row
        
    }else{
            echo 
    'TEST. In and Out emps are NOT the same <br/>';  // for testing
            
    if(!empty($inEmp)){
                
    $buffer.='<tr>'."\n";  // start a new row
                
    $buffer.='<td>'.$inValue.'</td>'// include the seporator column
                
    $buffer.='<td>-</td>'// include the seporator column
                
    $buffer.='<td>'.$outValue.'</td>'// include the seporator column
                
    $buffer.='<td>'.$inEmp.'</td>'// emp name
                
    $buffer.='</tr>'."\n";  // end the row
            
    }
            if(!empty(
    $outEmp)){
                
    $buffer.='<tr>'."\n";  // start a new row
                
    $buffer.='<td>'.$inValue.'</td>'// include the seporator column
                
    $buffer.='<td>-</td>'// include the seporator column
                
    $buffer.='<td>'.$outValue.'</td>'// include the seporator column
                
    $buffer.='<td>'.$outEmp.'</td>'// emp name
                
    $buffer.='</tr>'."\n";  // end the row
            
    }
        }    
    // end IF/ELSE($inEmp==$outEmp)...                
    }    // end WHILE($i<=$numRows)... 
    As you can see, in the "look for clock-OUT data" section, I try to break out of the IF and the sub-WHILE at the same time, and move into "putting it all together".

    NOTHING echos from that section.

    Any help is appreciated.
    ~ Mo

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Just break;
    Break cannot be used in an if statement by itself. Using this:
    PHP Code:
    $i 0;
    while (
    true)
    {
        if (++
    $i 10)
        {
            break;
        }
        echo 
    $i "<br />";
    }
    echo 
    'done'
    Results in:
    PHP Code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    done 
    So, same logic applies to this. You have two while loops, and you are in an if. If you use a break 2; it will halt 2 levels of loops, which would put you back to the main. Does that make sense?

    To push a little harder, I'd try to avoid using breaks outside of switches. Sometimes these can get a little messy to follow, but you never need them (except in a switch of course :P). What you do is add multiple controls to your loop instead:
    PHP Code:
    while($i<=$numRows){
        
    $bContinue true;
        while((
    $row mysql_fetch_array($resultMYSQL_ASSOC)) && $bContinue){
            
    // Do some stuffs:
            
    if($inOut==0){  // we are looking for an OUT time
                
    if(!$row['isIn']){  // if this is a Clock OUT ...
                    
    $outValue='<td>'.$time.'</td>';  // print the time
                    
    $outEmp=$empsAry[$empId]["fName"].' '.$empsAry[$empId]["mi"].' '.$empsAry[$empId]["lName"];  // set the name
                    
    $stop=$unixTime;  // capture it as a "STOP" time
                
    }else{  // we're looking for an OUT, but got an IN
                    
    $outValue='<td></td>';  // define as blank
                    
    $outEmp='';
                }  
    // end IF/ELSE(!$row['isIn'...                        
                
    $inOut++;  // flip the toggle so that we look for an IN next.
                // Now here, we are satisfied
                
    $bContinue false;
                
    // break 2;  // anytime we finish the OUT portion, we want to jump out and assemble what we've gathered
            
    }  // end IF($inout==='out')... 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    424
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Thanks-a-bunch.
    Yeah, my goal is to stop the pointer on the while(row) loop, and jump out to the main loop to assemble my info.

    I see how you achieved that with the $bContinue variable.
    I like that. That seems pretty smooth.

    Once again, thanks-a-bunch,
    ~ Mo

    NOTE: I'm off to the CodeWorks PHP convention right now, so the first chance I'll have to try this out will be late tonight. If I have any follow-up questions, I'll post them tomorrow.
    Edit:
    Oops!!!
    It turns out my mental calendar was a day off, and the convention is tomorrow.
    I guess I get to try this sooner now.
    Thanks again.
    Last edited by mOrloff; 11-10-2010 at 06:55 PM.

  • #4
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    424
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Well, even with all that help, I was still pulling my hair out trying to make it work.

    Soooo ... I thought that I should start over, scale down, and take it one small step at a time.
    I got it!!! (Well, with a down-sized light-weight sample, at least )
    Oh boy. What a relief!

    Here's my code. As you can see, it ended up going a completely different direction.
    PHP Code:
    $sql="SELECT id, empId, name, isIn FROM sandbox ORDER BY empId";
    $result=mysql_query($sql,$connection);

    $results=$output=array();  // instantiate
    while($row mysql_fetch_array($resultMYSQL_ASSOC)){
        
    $results[]=$row;
    }  
    // convert MySQL-Result into multidimensional array

    $lookingFor='in';  // instantiate a toggle to tell us whether to look for an IN or an OUT
    foreach($results as $row){
        
    $allDone=FALSE;  // we are not done with this itteration of the loop
    ### look for clock-IN data ###
        
    if($lookingFor==='in'){  // if we should be looking for an IN record...
            
    if($row['isIn']){  //...and this IS an IN record...
                
    $output[]="<tr>\n <td>In Time</td><td>-</td>";  //...assemble some info...
                
    $allDone=TRUE;  //...and show that we got good data from this loop itteration 
            
    }else{  // if we rcvd an OUT record when we are looking for an IN...
                
    $output[]="<tr>\n <td></td><td>-</td>";  //...assemble an empty IN set
            
    }
            
    $lookingFor='out';  // flip the toggle
        
    }
    ### look for clock-OUT data ###
        
    if(!$allDone && $lookingFor==='out'){  // if we we aren't satisfied and should be looking for an OUT record...
            
    if(!$row['isIn']){  //...and this IS an OUT record...
                
    $output[]='<td>Out Time</td><td>'.$row['name']."</td> \n </tr> \n";  //...assemble some info
            
    }else{  // if we rcvd an IN record when we are looking for an OUT
                
    $output[]='<td></td><td>'.$row['name'].'</td>'."</td> \n </tr> \n";  //...assemble an empty IN set
            
    }
            
    $lookingFor='in';  // flip the toggle
        
    }
    }
    echo 
    '<table>';  // open
    foreach($output as $key=>$value){echo $value;}  // show the fruits of our labors
    echo '</table>';  // close 
    I'd like to say that I'm posting this solution strictly for posterity sake, but the truth is I'm also kinda proud (and rather excited ).
    I've also attached the tiny sql for the sample table I developed/tested this with.
    Certainly, if you have a critique, please share. I'd be happy to see how I could improve it further still.

    Thanks-a-bunch,
    ~ Mo
    Attached Files Attached Files
    Last edited by mOrloff; 11-14-2010 at 12:38 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •