Support

Account

Home Forums Front-end Issues Mutliple choice checkbox, add a class

Helping

Mutliple choice checkbox, add a class

  • Hi there. I created the checkbox with three choices:
    Option1
    Option2
    Option3
    I’m trying to check this values in array to define the string variable $addclassexamgrid, that is sent to a class (through echo).
    Here is the code:

    <?php
    // check if the repeater field has rows of data
       if( have_rows('add_info_exam_grid') ): ?>
    
         <?php $checkboxarray = get_field('add_age_exam_grid'); ?>
         
         <?php if (($checkboxarray && in_array("Option1", $checkboxarray)) and ($checkboxarray && in_array("Option2", $checkboxarray)) and ($checkboxarray && in_array("Option3", $checkboxarray))) : ?>                                
         <?php $addclassexamgrid = 'class1 class2 class3'; ?>
    
         <?php elseif (($checkboxarray && in_array("Option1", $checkboxarray)) and ($checkboxarray && in_array("Option2", $checkboxarray))) : ?>
         <?php $addclassexamgrid = 'class1 class2'; ?>
    
         <?php elseif (($checkboxarray && in_array("Option1", $checkboxarray)) and ($checkboxarray && in_array("Option3", $checkboxarray))) : ?>
         <?php $addclassexamgrid = 'class1 class3'; ?>
    
         <?php elseif (($checkboxarray && in_array("Option2", $checkboxarray)) and ($checkboxarray && in_array("Option3", $checkboxarray))) : ?>
         <?php $addclassexamgrid = 'class2 class3'; ?>
    
         <?php elseif ($checkboxarray && in_array("Option1", $checkboxarray)) : ?>
         <?php $addclassexamgrid = 'class1'; ?>
    
         <?php elseif ($checkboxarray && in_array("Option2", $checkboxarray)) : ?>
         <?php $addclassexamgrid = 'class2'; ?>
    
         <?php elseif ($checkboxarray && in_array("Option3", $checkboxarray)) : ?>
         <?php $addclassexamgrid = 'class3'; ?>
    
         <?php endif; ?>
    
         <?php while ( have_rows('add_info_exam_grid') ) : the_row(); ?>
    
          <div class="grid-item <?php echo $addclassexamgrid; ?> col-md-4 col-sm-6 col-xs-12">
          //Some code here
          </div>
    
          <?php endwhile;
          else :
          // no rows found
          endif;
          ?>

    The problem is that the value of the variable $addclassexamgrid is not send to div class. Could anybody help?

  • What is the return value of the field set to? Your code assumes values, but you might want to check it. It could be somewhere in you if/elseif statements.

    I would simplify it like this

    
    <?php	
    	
    	if (have_rows('add_info_exam_grid')) {
    		$addclassexamgrid = array();
    		$checkboxarray = get_field('add_age_exam_grid');
    		foreach ($checkboxarray as $value) {
    			switch ($value) {
    				case 'Option1':
    					$addclassexamgrid[] = 'class1';
    					break;
    				case 'Option2':
    					$addclassexamgrid[] = 'class2';
    					break;
    				case 'Option3':
    					$addclassexamgrid[] = 'class3';
    					break;
    			} // end switch
    			while (have_rows('add_info_exam_grid')) {
    				the_row();
    				?>
    					<div class="grid-item <?php echo implode(' ', $addclassexamgrid); ?> col-md-4 col-sm-6 col-xs-12">
    						//Some code here
    					</div>
    				<?php 
    			} // end while have_rows
    		} // end foreach
    	} // end if have_rows
    ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Mutliple choice checkbox, add a class’ is closed to new replies.