title: php - lowest cost based on case sizes
body: <p>I'm working on a shipping module for wine, and was wondering if anyone could give me a hand - basically:</p>
  
  <p>The wine can be shipped in cases of 8, 12 or 15 bottles, each with its own price. The module needs to take the total number of bottles in the order, and work out which combination of cases gives the lowest price. Eg in an order of 31 bottles, the lowest price works out to 1 case of 15 and two cases of 8, (rather than 2 cases of 15 and 1 of 8, or 2 of 12 and one of 8). Currently, I have the following, which almost works, but misses a few possible combinations</p>
  
  <pre><code>    foreach ($rates as $case_size =&gt; $case_price) 
      {
        $price = floor($total_bottles / $case_size) * $case_price;
        $rem = $total_bottles % $case_size;
        if($rem &gt; 12)
        {
         //needs to use another case of 15 
         $price = $price + $rates[15];
        }
        elseif($rem &gt; 8)
        {
          //needs an extra case of 12
          $price = $price + $rates[12];
        }
        elseif($rem &gt; 0)
        {
          //needs an extra case of 8
          $price = $price + $rates[8];
        }
        $quotes[] = $price;
      }
  
      return min($quotes);
  </code></pre>