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>
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>
Share
Improve this question
edited Jun 25, 2012 at 23:18
hakre
199k55 gold badges450 silver badges856 bronze badges
asked Jun 25, 2012 at 21:03
Rocco The TacoRocco The Taco
3,79714 gold badges50 silver badges80 bronze badges
1
- 4 Unless you are seeing increments changing al the time (10 minutes, 5 minutes, etc.), you are aware that hardcoding this is fine, and faster the rerererecalculating this with PHP all the time? You could of course generate the list once with some PHP code, and leave that code as a ment near there for someone to quickly grab to recreate the list with slightly different variables in a year or so, after which it once again stays static. – Wrikken Commented Jun 25, 2012 at 21:17
4 Answers
Reset to default 3Probably 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 ing 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:
- 10-increment is easy, just multiply
$i
by ten. - For the displayed value, multiply by 15 to get the total amount of minutes. Then transform that into hours and minutes using the modulo operator (which gives you the minutes) and standard divison (to get the hours).
This should do it. Change the start number of $i to how many total minutes the dropdown should contain. It´s now set to 750 minutes.
echo '<select>';
for ($i = 750; $i >= 15; $i -= 15) {
$hours = $i / 60;
$min = $i % 60;
echo '<option>';
if ($hours >= 1)
echo floor($hours)." Hours ";
if ($min > 1)
echo $min." Minutes";
echo '</option>';
}
echo '</select>';
NOTE: This code is not perfect, the start number needs to be evenly divided with 15 to generate your desired result, (however, it works).
Something like:
<select size="1" name="labor" id="labor">
<?php
for ($x = 1; $x < 11; $x++) {
echo '<option value="';
echo $x*10;
echo '">';
echo $x*15;
echo " Minutes</option>";
}
?>
</select>
Throw in an if statement there to seperate it so that once (x/10*15)>60 it starts carrying over into hours instead of having 75/90/105/120 minutes.