PHP code example of nawebco / box-packer

1. Go to this page and download the library: Download nawebco/box-packer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nawebco / box-packer example snippets

$xslt

Co\BoxPacker\Packer;
use NAWebCo\BoxPacker\GenericPackable;


$packer = new Packer();

// Width, length, height, description
// The order of the dimensions doesn't matter.
$packer->addBox(new GenericPackable(10, 15, 6, 'Big box'));

// Width, length, height, description
// The order of the dimensions doesn't matter.
$packer->addItem(new GenericPackable(8.5, 11, 2, 'First book'));
$packer->addItem(new GenericPackable(8.5, 11, 1.2, 'Second book'));
$packer->addItem(new GenericPackable(3, 3, 3, 'Carved wooden block'));

$result = $packer->pack();

if( $result->success() ){
    echo "Everything fits!\n\n";

    foreach( $result->getPackedBoxes(true) as $boxResults ){
        $box = $boxResults['box'];
        $contents = $boxResults['contents'];

        $containerDescription = $box->getDescription() ?: 'One box';
        echo "$containerDescription contains:\n";

        foreach( $contents as $item ){
            $itemDescription = $item->getDescription() ?: 'Item';
            echo "$itemDescription ({$item->getWidth()}, {$item->getLength()}, {$item->getHeight()})\n";
        }
    }
} else {
    echo count($result->getNotPackedItems()) . " items didn't fit.";
}