While copying the code, I missed a line. This DOES work!
<?php
// Function to locate best value or higher
function best_rack($rack) {
$rackcabinetarray = array(12, 16, 20, 26, 32, 36, 42, 44, 47);
// See if rack matches exactly, if not use two panels
$index = 0;
foreach ($rackcabinetarray as $key=>$rackcabinet) {
if ($rackcabinet==$rack) {
// Rack number is in array, use it
return $rackcabinetarray[$key];
}
}
if ($index==0) {
// Use two racks
$rack = ceil($rack/2);
foreach ($rackcabinetarray as $key => $rackcabinet) {
if ($rackcabinet >= $rack) {
$index = $key;
break;
}
}
return $rackcabinetarray[$index] . "-" . $rackcabinetarray[$index];
}
}
// Test with exact values
echo "input: 36 = " . best_rack(36) . "<br>";
// Test with multiple value
echo "input: 40 = " . best_rack(40) . "<br>";
echo "input: 48 = " . best_rack(48) . "<br>";
echo "input: 57 = " . best_rack(57);
?>
Somehow I missed the the BREAK line. Also, you did NOT mention you might need more that two panels. I only set it up for one or two panels. For more than two panels, you can divide by 3 or more as needed. This code would work. You would just figure out the max number of panels first and then divide by that number. To create the max number of panels, you would find the division results and add one to it. Should work. Here is a better example that allows any number of panels: ( And is smaller code! )
<?PHP
// Function to locate best value or higher
function best_rack($rack) {
$rackcabinetarray = array(12, 16, 20, 26, 32, 36, 42, 44, 47);
// Locate maximum number of panels needed
$max = ceil( $rack / max($rackcabinetarray) );
// Locate correct rack
$rack = ceil($rack/$max);
foreach ($rackcabinetarray as $key => $rackcabinet) {
if ($rackcabinet >= $rack) {
return $rackcabinetarray[$key] . " ( times " . $max . " )";
}
}
}
// Test with exact values
echo "input: 36 = " . best_rack(36) . "<br>";
// Test with multiple value
echo "input: 40 = " . best_rack(40) . "<br>";
echo "input: 48 = " . best_rack(48) . "<br>";
echo "input: 57 = " . best_rack(57) . "<br>";
echo "input: 127 = " . best_rack(127) . "<br>";
echo "input: 249 = " . best_rack(249);
?>