|
| 1 | +<?php |
| 2 | + |
| 3 | +use PhpOffice\PhpSpreadsheet\Calculation\Engineering\ConvertUOM; |
| 4 | +use PhpOffice\PhpSpreadsheet\Helper\Sample; |
| 5 | +use PhpOffice\PhpSpreadsheet\Settings; |
| 6 | + |
| 7 | +require __DIR__ . '/../../Header.php'; |
| 8 | + |
| 9 | +$helper = new Sample(); |
| 10 | +if ($helper->isCli()) { |
| 11 | + $helper->log('This example should only be run from a Web Browser' . PHP_EOL); |
| 12 | + |
| 13 | + return; |
| 14 | +} |
| 15 | + |
| 16 | +$categories = ConvertUOM::getConversionCategories(); |
| 17 | +$units = []; |
| 18 | +foreach ($categories as $category) { |
| 19 | + $categoryUnits = ConvertUOM::getConversionCategoryUnitDetails($category)[$category]; |
| 20 | + $categoryUnits = array_unique( |
| 21 | + array_combine( |
| 22 | + array_column($categoryUnits, 'unit'), |
| 23 | + array_column($categoryUnits, 'description') |
| 24 | + ) |
| 25 | + ); |
| 26 | + $units[$category] = $categoryUnits; |
| 27 | +} |
| 28 | + |
| 29 | +?> |
| 30 | +<form action=Convert-Online.php method="POST"> |
| 31 | + <div class="mb-3 row"> |
| 32 | + <label for="category" class="col-sm-2 col-form-label">Category</label> |
| 33 | + <div class="col-sm-10"> |
| 34 | + <select name="category" class="form-select" onchange="this.form.submit()"> |
| 35 | + <?php foreach ($categories as $category) { |
| 36 | + echo "<option value=\"{$category}\" " . ((isset($_POST['category']) && $_POST['category'] === $category) ? 'selected' : '') . ">{$category}</option>", PHP_EOL; |
| 37 | + } ?> |
| 38 | + </select> |
| 39 | + </div> |
| 40 | + </div> |
| 41 | + <div class="mb-3 row"> |
| 42 | + <label for="quantity" class="col-sm-2 col-form-label">Quantity</label> |
| 43 | + <div class="col-sm-10"> |
| 44 | + <input name="quantity" type="text" size="8" value="<?php echo (isset($_POST['quantity'])) ? htmlentities($_POST['quantity'], Settings::htmlEntityFlags()) : '1.0'; ?>"> |
| 45 | + </div> |
| 46 | + </div> |
| 47 | + <div class="mb-3 row"> |
| 48 | + <label for="fromUnit" class="col-sm-2 col-form-label">From Unit</label> |
| 49 | + <div class="col-sm-10"> |
| 50 | + <select name="fromUnit" class="form-select"> |
| 51 | + <?php foreach ($units[$_POST['category']] as $fromUnitCode => $fromUnitName) { |
| 52 | + echo "<option value=\"{$fromUnitCode}\" " . ((isset($_POST['fromUnit']) && $_POST['fromUnit'] === $fromUnitCode) ? 'selected' : '') . ">{$fromUnitName}</option>", PHP_EOL; |
| 53 | + } ?> |
| 54 | + </select> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + <div class="mb-3 row"> |
| 58 | + <label for="toUnit" class="col-sm-2 col-form-label">To Unit</label> |
| 59 | + <div class="col-sm-10"> |
| 60 | + <select name="toUnit" class="form-select"> |
| 61 | + <?php foreach ($units[$_POST['category']] as $toUnitCode => $toUnitName) { |
| 62 | + echo "<option value=\"{$toUnitCode}\" " . ((isset($_POST['toUnit']) && $_POST['toUnit'] === $toUnitCode) ? 'selected' : '') . ">{$toUnitName}</option>", PHP_EOL; |
| 63 | + } ?> |
| 64 | + </select> |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + <div class="mb-3 row"> |
| 68 | + <div class="col-sm-10"> |
| 69 | + <input class="btn btn-primary" name="submit" type="submit" value="Convert"><br /> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | +</form> |
| 73 | + |
| 74 | +<?php |
| 75 | +/** If the user has submitted the form, then we need to calculate the value and display the result */ |
| 76 | +if (isset($_POST['submit'])) { |
| 77 | + $quantity = $_POST['quantity']; |
| 78 | + $fromUnit = $_POST['fromUnit']; |
| 79 | + $toUnit = $_POST['toUnit']; |
| 80 | + $result = ConvertUOM::CONVERT($quantity, $fromUnit, $toUnit); |
| 81 | + |
| 82 | + echo "{$quantity} {$units[$_POST['category']][$fromUnit]} is {$result} {$units[$_POST['category']][$toUnit]}", PHP_EOL; |
| 83 | +} |
0 commit comments