Check If bootstrap last column is even or odd

Im new to this forum and new to php too… hope i get some help…
i have bootstrap column in while loop, what i want is if bootstrap last column is odd then i want column to be 12 (col-12), i found the way to check number even or odd but want to check last number so if last number (last column) is odd i want column to be 12 else remain col-6 i have tried:

<div class"<?php echo ($i == (2 || 4 || 6) )?'col-md-6':'col-md-12'; ?>">
       display content there in while loop

   <div class" <?php echo ($i % 2 == 0)?'col-md-6':'col-md-12'; ?>">
   display content there in while loop

what’s your while condition; in other words, how do you know when you’re at the last column?

Im using ACF Custom field in Wordpress and Im using Acf repeater, so Acf repeater tell the number column

right, but the repeater must be given a data input, that tells it what columns to create, right?

yes,you are right,so when user input data and if column create is odd then odd column should col-12. (last column only) but if last column create is even then it should remain col-6. but following code is making every odd column = col-12 but i want last column = col-12 only if last column is odd
here is code below:

<?php if(have_rows('field_name')):?>
		$i = 1;
		$total= count(get_field ('field_name'));
		<div class="<?php echo ($i % 2 == 0 )?'col-md-6':'col-md-12'; ?>" id="<?php echo $i; ?>">
			<h1><?php the_sub_field('services_title'); ?></h1>

	<?php $i++; endwhile; ?>
<?php endif;?>

So $i is redundant - see get_row_index

okay, so… let’s move that OUTSIDE the loop.
That should give you the total number of rows.
If your row index = the number of rows, you’re on your last row.
If your row index = the number of rows AND it’s an odd number, output col-md-12. Otherwise output col-md-6.

is this right pattern for code, being new not sure but your solution did the trick, im grateful towards you, thank you

<div class="<?php echo ($i == $total & $total % 2 !== 0 )?'col-md-12':'col-md-6'; ?>" id="<?php echo $i; ?>">
	<h1><?php  echo " content";?></h1>

And in this case doesn’t matter, but is generally && (& is a Bitwise AND, which works when you’re using booleans, but can screw you up sometimes if you’re not careful), but yes, that would be the correct logic.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.