title: Calculate a rough estimate for shipping box size
body: <p>I'm trying to find the best way to calculate the box size needed for shipping.</p>
  
  <p>I have 3 shipping containers with different sizes. I have the product's width, length, depth, and mass defined in the database.</p>
  
  <p>I would like to know how to find the smallest amount of boxes needed to ship, and also the smallest dimensions of those boxes given the number of items in the cart.</p>
  
  <p>My current 'idea' is to find the maximum width of the entire products array, the select a box according to it, and then split the order as needed... this doesn't seem like it would work.</p>
  
  <p>My Box sizes are:
   - 8 x 6 x 6 = 228 cubic inches
   - 10 x 8 x 8 = 640 cubic inches
   - 12.5 x 12.5 x 12.5 = 1953.125 cubic inches</p>
  
  <p>A product is defined as such:</p>
  
  <pre><code> [Product] =&gt; Array
                  (
                      [STOCK_CODE] =&gt; 010003
                      [Product_Slug] =&gt; GABA_010003
                      [ItemName] =&gt; GABA
                      [WHOLESALE_PRICE] =&gt; 17.47
                      [RETAIL_PRICE] =&gt; 24.95
                      [Brand] =&gt; 
                      [ProductLine] =&gt; 
                      [image_name] =&gt; 705077000440
                      [MASS] =&gt; 0.313
                      [Height] =&gt; 4.625
                      [Width] =&gt; 2.375
                      [Depth] =&gt; 2.375
                      [cubic_inches] =&gt; 26.087890625
                  )
  </code></pre>
  
  <p>I've looked into knapsack problem, packing problem, etc and can't find a way to do this. Any help would be GREAT.</p>
  
  <pre><code>function shipping(){
  
          $this-&gt;CartProduct-&gt;unbindModel(
              array('belongsTo' =&gt; array('User'))
          );
  
          //find all cart products by current logged in user
          $cartItems = $this-&gt;CartProduct-&gt;find('all', array('conditions' =&gt; array('CartProduct.user_id' =&gt; $this-&gt;Auth-&gt;user('id'))));
  
          $i = 0;
  
          //get the max width, height, depth
          $maxHeight = 0;
          $maxWidth = 0;
          $maxDepth = 0;
          foreach($cartItems as $c){
              $cartItems[$i]['Product']['cubic_inches'] = $c['Product']['Height'] * $c['Product']['Width'] * $c['Product']['Depth'];
              $cartItems[$i]['CartProduct']['total_cubic_inches'] = ($c['Product']['Height'] * $c['Product']['Width'] * $c['Product']['Depth']) * $c['CartProduct']['qty'];
  
              if($c['Product']['Height'] &gt; $maxHeight)
              {
                  $maxHeight = $c['Product']['Height'];
              }
  
              if($c['Product']['Width'] &gt; $maxWidth)
              {
                  $maxWidth = $c['Product']['Width'];
              }
              if($c['Product']['Depth'] &gt; $maxDepth)
              {
                  $maxDepth = $c['Product']['Depth'];
              }
              $i++;
          }
  
          //possible containers 
          //8 x 6 x 6 = 228 ci
          //10 x 8 x 8 = 640 ci
          //12.5 x 12.5 x 12.5 = 1953.125
  
          $possibleContainers = array(
              1 =&gt; array(
                  'Height' =&gt; 8,
                  'Width' =&gt; 6,
                  'Depth' =&gt; 6,
                  'Cubic' =&gt; 228),
              2 =&gt; array(
                  'Height' =&gt; 10,
                  'Width' =&gt; 8,
                  'Depth' =&gt; 8,
                  'Cubic' =&gt; 640),
              3 =&gt; array(
                  'Height' =&gt; 12.5,
                  'Width' =&gt; 12.5,
                  'Depth' =&gt; 12.5,
                  'Cubic' =&gt; 1953.125)
          );
  
  
  
          $max = array(
              'Height' =&gt; $maxHeight, 
              'Width' =&gt; $maxWidth, 
              'Depth' =&gt; $maxDepth, 
          );
  
          pr($cartItems);
          pr($possibleContainers);
          die();  
      }
  </code></pre>