I have a standard form array in PHP like this. I need to account for up to 10 hours of labor in 15 minute increments with 10 as a value for every 15 min or 40 per hour. Is it possible to automate this into some sort of array with PHP instead of hardcoding each of these values? It just seems like there should be a better way and I have no idea how to start?
<select size="1" name="labor" id="labor">
<option value="80">2 Hours</option>
<option value="70">1 Hour 45 min.</option>
<option value="60">1 Hour 30 min.</option>
<option value="50">1 Hour 15 min.</option>
<option value="40">1 Hour</option>
<option value="30">45 Minutes</option>
<option value="20">30 Minutes</option>
<option value="10">15 Minutes</option>
</select>
Probably easiest to hardcode the solution. I think this is one of those situations where it is OK as @Wrikken mentioned, as it makes the code very clean and easy to maintain (Imagine coming back to this in a year or two). In addition this situation can also be handled very well with a database table.
First use an array to store you values and descriptions:
$list = array();
$list['10'] = '15 Minutes';
....
Then loop through the entire array to generate your dropdown:
<select size="1" name="labor" id="labor">
<?php
foreach($list as $value => $desc){
$option = "<option value=$value>" . $desc . "</option>";
echo $option;
}
?>
</select>
You need two values, the 10-units increment and the user-displayed value. Run a for
loop over $i
(or some other variable) from 1 to 40 and calculate your values inside the loop:
$i
by ten.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With