If Statement to count total number of personnel assigned a category and assign html layout based on total

7 posts by 3 authors in: Forums > CMS Builder
Last Post: January 28, 2021   (RSS)

By Mikey - December 31, 2020

Howdy folks,

I have a multi section editor where I create records for staff personnel. Each staff member is assigned a category such as, "founder = value 4, employee = value 1, administrative = value 2, sales = value 3". I'm trying to figure out a way to count how many staff members are assigned the category of "founder" with the value of "4", and if there are more than two staff members assigned the category value of "4" then show in two columns in a row, else show the single staff member in one column within a single row.

I'm struggling to figure out how to show one or two columns in a row. Any suggestions?

I hope all that makes since.

<?php foreach ($staffRecords as $key => $record): ?>
            
    <?php if($record['categories'] == '4'): ?>
      
        <?php if ($key > 0): ?>
            
            <article class="small-6 medium-6 large-6 cell">
            	Show all the staff members assign to category #4 in Two Columns within each Row (<?php echo $key; ?>)
            </article>
            
        <?php else: ?>
            
            <article class="small-12 medium-12 large-12 cell">
            	Show the singular staff member assign to category #4 in One Column within a single Row (<?php echo $key; ?>)
		But do not show this if there are more than one staff member which is assign to category #4.
            </article>
            
        <?php endif; ?>
            
    <?php endif; ?> 

<?php endforeach ?>

Thanks for any insight.

Zicky

Hey Zicky.

Could you use the following to count the number of records.

<?php
  list($sectionRecords, $sectionMetaData) = getRecords(array(
    'tableName'   => 'section',
    'loadUploads' => true,
    'allowSearch' => false,
  ));
  
  echo $sectionMetaData['totalRecords'];
?>

Then use the $sectionMetaData['totalRecords'] for the if statement?

Or maybe some variation of this post: https://www.interactivetools.com/forum/forum-posts.php?postNum=2244131#post2244131

Cheers,

Tim (toledoh.com.au)

By Jenna - January 27, 2021

Hi Zicky,

Did Tim's guidance help you get to an acceptable solution? If not, we'd be happy to try to guide you to a solution that works for you.

Please let us know if we can help you.

Jenna Cooke - PHP Programmer
interactivetools.com

By Mikey - January 27, 2021

No, I wasn't able to get this working. I tried Toledoh's suggestion, but there was something that prevented me from being able to get it to work... but I can't remember what it was. Any suggestions are appreciated. 

Zicky

By Jenna - January 28, 2021

Hi Zicky,

I think I may have a solution for you. In /lib/common.php we have the following function:

// array_groupBy:
//   eg. $recordsByNum = array_groupBy($records, 'num');
//   eg. $recordsByCategory = array_groupBy($records, 'category', true);
//       foreach ($recordsByCategory as $category => $categoryRecords) { ;;; }
// PHP Alternate: $recordsByNum = array_combine(array_column($menuRecords, 'num'), $menuRecords);
function array_groupBy($recordList, $indexField, $resultsAsArray = false) {

It allows you to group the returned results into an associative array with the column of your choice. In your case you may use it like:

$groupedStaffRecords = array_groupBy($staffRecords, 'categories', true);

Putting that line beneath your $staffRecords list near the top of the file. You would then loop through the $groupedStaffRecords using a foreach loop:

<?php foreach ($groupedStaffRecords as $category => $records) :?> 
  <?php if($category == '4'): ?>
     <?php $total_staff = count($records); // Set a total variable to use for the columns?>
     <?php foreach ($records as $key => $record): // this is the loop of the individual records in this category ?>
       <!--below it checks if it's the last result and is even (% 2) modulus operator checks if it is divisible by the number -->
       <?php if ($key == $total_staff && $total_staff % 2 !== 0) : ?>
         <div class="small-12 medium-12 large-12 cell">
       <?php else: ?>
         <div class="small-6 medium-6 large-6 cell">
       <?php endif ?>
           <!-- show record content -->
         </div>
     <?php endforeach ?> 
  <?php endif ?>
<?php endforeach ?>

You'll probably have to modify some things to ensure they work for your specific use case, but this should help to get you started in the right direction.

Let me know if this helps or if I need to clarify anything.

Jenna Cooke - PHP Programmer
interactivetools.com

By Mikey - January 28, 2021

Jenna,

Thank you for all your help. I finally got this working and it's doing a great job at what I was trying to achieve. I attached a jpg that shows what this does. Here's the end results.

<!-- This layout is built on Foundation 6 --> 
    
<div class="grid-container">
   <div class="grid-x grid-margin-x" data-equalizer>

<?php $groupedStaffRecords = array_groupBy($staffRecords, 'categories', true); ?>

<?php foreach ($groupedStaffRecords as $category => $records) :?> 
  <?php if($category == '4'): ?>

     <?php $total_staff = count($records); // Set a total variable to use for the columns ?>
     <?php $x = 2; // Define the threshold for when to trigger whether you go with one column in a row or two columns ?>
            
     <?php foreach ($records as $key => $record): // this is the loop of the individual records in this category ?>

         <?php if ($total_staff >= $x) : ?>
            
            <article class="small-12 medium-6 cell image-fit staff founders">
                <?php if($record['personnel_photo']): ?>
                <a href="<?php echo $record['_link'] ?>" alt="<?php echo htmlencode($record['title']) ?>">
                    <?php foreach ($record['personnel_photo'] as $index => $upload): ?>
                        <img src="<?php echo htmlencode($upload['thumbUrlPath4']) ?>" alt="<?php echo htmlencode($record['title']) ?>">
                    <?php endforeach ?>
                </a>
                <?php endif; ?>
                <div class="gray-panel-container">
                <h2><a href="<?php echo $record['_link'] ?>" alt="<?php echo htmlencode($record['title']) ?>"><?php echo htmlencode($record['title']) ?> <?php echo htmlencode($record['last_name']) ?></a></h2>
                <h4><?php echo htmlencode($record['business_title']) ?></h4>
                  <div data-equalizer-watch>
                  <?php if($record['summary']): ?>
                    	<?php echo htmlencode($record['summary']) ?>
                  <?php else: ?>
                    	<?php echo $record['content']; ?>
               	  <?php endif; ?>
                  </div>
                </div>
            </article>
            
       <?php else: ?>
            
            <article class="small-12 medium-5 large-4 cell image-fit staff founders">
                <?php if($record['personnel_photo']): ?>
                <a href="<?php echo $record['_link'] ?>" alt="<?php echo htmlencode($record['title']) ?>">
                    <?php foreach ($record['personnel_photo'] as $index => $upload): ?>
                        <img src="<?php echo htmlencode($upload['thumbUrlPath4']) ?>" alt="<?php echo htmlencode($record['title']) ?>">
                    <?php endforeach ?>
                </a>
                <?php endif; ?>
            </article>
            <article class="small-12 medium-7 large-8 cell image-fit staff founders add_margin_double_bottom">    
                <div class="gray-panel-container">
                <h2><a href="<?php echo $record['_link'] ?>" alt="<?php echo htmlencode($record['title']) ?>"><?php echo htmlencode($record['title']) ?> <?php echo htmlencode($record['last_name']) ?></a></h2>
                <h4><?php echo htmlencode($record['business_title']) ?></h4>
                  <div data-equalizer-watch>
                  <?php if($record['summary']): ?>
                    	<?php echo htmlencode($record['summary']) ?>
                  <?php else: ?>
                    	<?php echo $record['content']; ?>
                  <?php endif; ?>
                  </div>
                </div>
            </article>
            
       <?php endif ?>

     <?php endforeach ?> 

  <?php endif ?>
<?php endforeach ?>            
            
            
   <!--/grid-x--></div>
<!--/grid-container--></div>

Thanks, Zicky

Attachments:

example-of-layout.jpg 176K