Bako Gdaniec
Bako Gdaniec

Reputation: 49

Check how much left to the nearest multiple of four in PHP

I am creating a magento website where I'm allowing to buy at least 4 products and then multiples of four. The thing I'm struggling with is the else statement which should give a number to the following multiple of four. For e.g. If the person has 5 products it should display "Add 3 more products to your bundle" , or if person has 10 products " Add 2 more products to your bundle". I hope I've described it well.

My code is :

      `<?php else: ?>
        <p class="empty"><?php echo $this->__('Add  more products to your bundle before checking out') ?></p>
          <?php endif ?>`

Thank you for any helpful answers.

Upvotes: 0

Views: 51

Answers (2)

Tanuel Mategi
Tanuel Mategi

Reputation: 1283

You are probably looking for the modulus-operator

$modulo = 11 % 4; //this will return 3 as the remainder
echo $modulo; //echos 3
if($modulo!=0){ //only execute this when items are needed
    $needed = 4 - $modulo;
    echo "you need ". $needed ." more items"; //echos "you need 1 more items
}

Upvotes: 2

user2959229
user2959229

Reputation: 1355

You're looking for the modulus function.

$remainder = ($item_count % 4);

$a % $b | Modulus | Remainder of $a divided by $b.

Upvotes: 1

Related Questions