#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    29

    Insert empty div if Group has less than even rows returned


    On this page I have a bunch of contact information for each individual employee in the company:


    Everything is grouped together properly except when a category has an odd number of employees. The first employee for the next category is added to the previous category.

    How do I fix this, so that if a category only has three employees the fourth div cell in the group is generated with a blank div cell?

    PHP Code:
            if($staffQuery=mysqli_query($staffConnected"SELECT `name`, `email`, `department`, `position` FROM `staff` WHERE `class`=2 ORDER BY `department`, `name` ASC")){
            
    $rowNum=1;
            
    $cellNum=0;
            
    $amountOfStaffReturned=mysqli_num_rows($staffQuery);
                if(
    $amountOfStaffReturned>0){
                
    $departmentHeader=false;
                
    $content.='<div class="emailHeader">Staff Contacts</div>';
                    while(
    $staff=mysqli_fetch_array($staffQueryMYSQLI_ASSOC)){
                    
    $staffName=$staff['name']; $staffEmail=$staff['email']; $staffDepartment=$staff['department']; $staffPosition=$staff['position'];
                    
    $rowName=rowName($rowNum);
                        if(
    $rowNum>1){$rowEmailClassAdd=' emailRow';}else{$rowEmailClassAdd='';}
                        if(
    $cellNum%2==0){
                        if(
    $cellNum!=0){$content.='</div>';}else{}
                        if(
    $departmentHeader!=$staffDepartment){$content.='<div class="departmentHeader">'.$staffDepartment.'</div>'$departmentHeader=$staffDepartment;}else{}
                        
    $content.='<div class="overflowclear'.$rowEmailClassAdd.'" id="emailStaff'.ucfirst($rowName).'Row">';
                        
    $rowNum++;
                        }else{}
                    
    $content.='
                    <div class="emailCell">
                    <div class="emailTitle">'
    .$staffName.'</div>
                    <div class="emailSubTitle">'
    .$staffPosition.'</div>
                    <div><a class="contactemail" href="mailto:'
    .$staffEmail.'">'.escapeemail($staffEmail).'</a></div>
                    </div>'
    ;
                        if((
    $amountOfStaffReturned%2!=&& $cellNum+1==$amountOfStaffReturned)){
                        
    $content.='<div class="emailCell">
                        &nbsp;
                        </div>
                        </div>'
    ;
                        }else{}
                    
    $cellNum++;
                    }
                }else{}

    To be clearer, the Assistants group should only have three employees in it, Information Technology should also have three (Dennis O'Neal) should be moved down to the IT group, the Post-Production group should have three, the Production group should have four, and the sales group should have three.
    Last edited by HDFilmMaker2112; July 21st, 2013 at 06:07 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,619
    Rep Power
    595
    After your loop, add a check to see if $cellNum is even. If it is then that means it was odd just before exiting. If so then add your blank entry.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    29
    I'm not sure I really follow...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,619
    Rep Power
    595
    Think about the value of $cellNum. You indicated your problem comes when you have an odd number of entries. That is, when your loop exits on the 3rd entry, you want to output a blank entry, right? The same would be true if you have 1 entry or 5 or any odd number. At the end of the loop you increment $cellNum so outside the loop the if the value of $cellNum is even (remainder function $cellNum%2==0) then on the last pass through the loop $cellNum must have been odd (the last increment made it even upon exiting the loop). So just check that remainder function and if odd (an even number of entries in the loop) do nothing and if even (an odd number of entries in the loop) output your blank entry.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    29
    Originally Posted by gw1500se
    Think about the value of $cellNum. You indicated your problem comes when you have an odd number of entries. That is, when your loop exits on the 3rd entry, you want to output a blank entry, right? The same would be true if you have 1 entry or 5 or any odd number. At the end of the loop you increment $cellNum so outside the loop the if the value of $cellNum is even (remainder function $cellNum%2==0) then on the last pass through the loop $cellNum must have been odd (the last increment made it even upon exiting the loop). So just check that remainder function and if odd (an even number of entries in the loop) do nothing and if even (an odd number of entries in the loop) output your blank entry.
    The issue is it doesn't exit on the odd cell. It has 24 rows of data pulled from the db call that execute until the 24th cell is generated... the headers for each section are input via a flag by doing this:

    Code:
    $departmentHeader=false;
    if($departmentHeader!=$staffDepartment){$content.='<div class="departmentHeader">'.$staffDepartment.'</div>'; $departmentHeader=$staffDepartment;}else{}
    As it is, I just bypassed all of this PHP code and just went with straight HTML... I would prefer a PHP solution because it'll let me update the database and insert or remove people easier; but if need be I'll go with pure HTML.
    Last edited by HDFilmMaker2112; July 21st, 2013 at 11:29 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,619
    Rep Power
    595
    Perhaps I am not understanding the issue. I thought if you have 3 employees, you needed a blank entry. I guess you need to clarify. I am not following the HTML vs PHP comment at all.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    29
    Code:
    SELECT `name`, `email`, `department`, `position` FROM `staff` WHERE `class`=2 ORDER BY `department`, `name` ASC
    My query pulls 24 entries from the database:




    My while loop runs through all 24 entries without stopping at any point.

    Each category header is added via a PHP flag where by I check the current value of the `department` column against the `department` column of the prior row.

    Code:
    if($departmentHeader!=$staffDepartment){$content.='<div class="departmentHeader">'.$staffDepartment.'</div>'; $departmentHeader=$staffDepartment;}else{}
    If the `department` column is different than the prior row, it generates the department header for those cells, if the `department` column is the same as the prior row, it does nothing.

    In the above database result set, my while loop does not end until it gets to the "Li Feng" row.


    Because of this, the cells are being populated without any kind of way to interrupt them to add the blank cell... at least not that I'm seeing.
    Last edited by HDFilmMaker2112; July 21st, 2013 at 03:44 PM.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,619
    Rep Power
    595
    I think I see now and my suggestion still applies with a slight modification within the loop. Initialize a new variable and when the department is the same as previous just increment it and output your entry. When the department changes do not increment it but check to see if it is odd or even. If even, output just the new entry and if odd generate a blank entry then output the new entry. In either case when the department changes, reinitialize the variable.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    29
    Originally Posted by gw1500se
    I think I see now and my suggestion still applies with a slight modification within the loop. Initialize a new variable and when the department is the same as previous just increment it and output your entry. When the department changes do not increment it but check to see if it is odd or even. If even, output just the new entry and if odd generate a blank entry then output the new entry. In either case when the department changes, reinitialize the variable.
    That definitely makes sense. We've made progress... the cell that needs to be moved is removed and replaced with the blank cell... however, the removed cell isn't placed in to it's proper department... it just disappears. I'm probably missing something here but not sure what...

    PHP Code:
        if($staffQuery=mysqli_query($staffConnected"SELECT `name`, `email`, `department`, `position` FROM `staff` WHERE `class`=2 ORDER BY `department`, `name` ASC")){ 
            
    $rowNum=1
            
    $cellNum=0
            
    $checkBlank=0;
            
    $amountOfStaffReturned=mysqli_num_rows($staffQuery); 
                if(
    $amountOfStaffReturned>0){
                
    $departmentHeader=false
                
    $content.='<div class="emailHeader">Staff Contacts</div>'
                    while(
    $staff=mysqli_fetch_array($staffQueryMYSQLI_ASSOC)){
                    
    $staffName=$staff['name']; $staffEmail=$staff['email']; $staffDepartment=$staff['department']; $staffPosition=$staff['position']; 
                    
    $rowName=rowName($rowNum); 
                        if(
    $rowNum>1){$rowEmailClassAdd=' emailRow';}else{$rowEmailClassAdd='';} 
                        if(
    $cellNum%2==0){
                        if(
    $cellNum!=0){$content.='</div>';}else{} 
                        if(
    $departmentHeader!=$staffDepartment){$content.='<div class="departmentHeader">'.$staffDepartment.'</div>';$departmentHeader=$staffDepartment;}else{} 
                        
    $content.='<div class="overflowclear'.$rowEmailClassAdd.'" id="emailStaff'.ucfirst($rowName).'Row">'
                        
    $rowNum++; 
                        }else{} 
                            if(
    $departmentHeader==$staffDepartment){
                            
    $checkBlank++;
                            
    $content.=
                            <div class="emailCell"> 
                            <div class="emailTitle">'
    .$staffName.'</div> 
                            <div class="emailSubTitle">'
    .$staffPosition.'</div> 
                            <div><a class="contactemail" href="mailto:'
    .$staffEmail.'">'.escapeemail($staffEmail).'</a></div> 
                            </div>'
    ;
                            }
                            elseif(
    $departmentHeader!=$staffDepartment){
                                if(
    $checkBlank%2==0){
                                
    $content.='<div class="emailCell"> 
                                <div class="emailTitle">'
    .$staffName.'</div> 
                                <div class="emailSubTitle">'
    .$staffPosition.'</div> 
                                <div><a class="contactemail" href="mailto:'
    .$staffEmail.'">'.escapeemail($staffEmail).'</a></div> 
                                </div>'
    ;
                                }
                                else{
                                
    $content.='<div class="emailCell"> 
                                &nbsp; 
                                </div>'
    ;
                                }
                            
    $checkBlank=0;
                            }else{}
                    
    $cellNum++;
                    }
                
    $content.='</div>';
                }else{} 

  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,619
    Rep Power
    595
    I think you need to change the order of things. When you detect a department change, you immediately check $checkBlank. If odd output the blank entry. Then output the new entry since it goes regardless of the value of $checkBlank.

    P.S. As a matter of programming practice, drop the else {} code. It is unnecessary and confusing.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    29
    Finally got it:

    PHP Code:
    $rowNum=1;  
                
    $cellNum=0;
                
    $loopNum=0;
                
    $amountOfStaffReturned=mysqli_num_rows($staffQuery);  
                if(
    $amountOfStaffReturned>0){
                
    $departmentHeader=false;  
                
    $content.='<div class="emailHeader">Staff Contacts</div>';  
                    while(
    $staff=mysqli_fetch_array($staffQueryMYSQLI_ASSOC)){ 
                    
    $staffName=$staff['name']; $staffEmail=$staff['email']; $staffDepartment=$staff['department']; $staffPosition=$staff['position'];  
                    
    $rowName=rowName($rowNum);  
                        if(
    $rowNum>1){$rowEmailClassAdd=' emailRow';}else{$rowEmailClassAdd='';}
                        
                        if(
    $departmentHeader!=$staffDepartment){
                            if(
    $cellNum%2!=0){
                            
    $content.='<div class="emailCell">&nbsp;</div>';
                            
    $cellNum++;
                            }
                            if(
    $departmentHeader!=false){
                            
    $content.='</div>';
                            }
                            
    $departmentHeader=$staffDepartment;
                            
    $content.='<div class="departmentHeader">'.$staffDepartment.'</div>';
                        }
                        else{if(
    $cellNum%2==0){$content.='</div>';}}
                        if(
    $cellNum%2==0){
                        
    $content.='<div class="overflowclear'.$rowEmailClassAdd.'" id="emailStaff'.ucfirst($rowName).'Row">';
                        
    $rowNum++; 
                        }
                        
    $content.='  
                        <div class="emailCell">  
                        <div class="emailTitle">'
    .$staffName.'</div>  
                        <div class="emailSubTitle">'
    .$staffPosition.'</div>  
                        <div><a class="contactemail" href="mailto:'
    .$staffEmail.'">'.escapeemail($staffEmail).'</a></div>  
                        </div>'
    ;
                    
    $cellNum++;
                    
    $loopNum++;
                        if(
    $cellNum%2!=&& $loopNum==$amountOfStaffReturned){
                        
    $content.='<div class="emailCell">&nbsp;</div>';                
                        }
                    }
                
    $content.='</div>';               
                } 

IMN logo majestic logo threadwatch logo seochat tools logo