echo '<p>Item #' . $i . ' ==> Weight: <input type="number" name="items[' . $i . '][weight]"/> | Price: <input type="number" name="items[' . $i . '][price]"/></p>'; } } ?> <input type="submit"/> </form> <hr/> <?php if (!isset($_REQUEST['items'])) { die; } $st = new store(); $pack = new knapsack($_REQUEST['capacity']); $allowFraction = isset($_REQUEST['fraction']); foreach ($_REQUEST['items'] as $index => $item) { $st->addItem(new item($item['weight'], $item['price'])); } print_knapsack($pack, 'Before'); print_store($st, 'Before'); echo '<br/>'; $st->sortItemsByValue(); print_store($st, 'Sorted'); echo '<br/>'; foreach ($st->getAllItems() as $index => $item) { if ($pack->getFreeSpace() >= $item->getWeight()) { $pack->addItem($st->pickItemByIndex($index)); } else { if (!$allowFraction) { continue; } $pack->addItem($st->pickItemFractionByIndex($index, $pack->getFreeSpace()));