Dynamic selection array in PHP

I have a standard array of forms in PHP like this. I have to account for up to 10 hours of work with a 15-minute increment with 10 as the value for every 15 minutes or 40 per hour. Is it possible to automate this into some kind of array with PHP instead of hard coding each of these values? There seems to 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>
+5
source share
4 answers

, . , , , @Wrikken, (, ). , .

:

$list = array();
$list['10'] = '15 Minutes';
....

, :

<select size="1" name="labor" id="labor">
<?php
   foreach($list as $value => $desc){
     $option = "<option value=$value>" . $desc . "</option>";
     echo $option;
   }
?>
</select>
+2

: 10 . for $i ( ) 1 40 :

  • 10- , $i .
  • 15, . , modulo ( ) ( ).
+2

That should do it. Change the start number $ i to the number of minutes that the drop-down list should contain. Now it is set for 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 must be divided by 15 in order to obtain the desired result (however, it works).

+1
source

Sort of:

<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>

Insert the if statement to separate it so that once (x / 10 * 15)> 60 it starts the transfer in hours instead of 75/90/105/120 minutes.

0
source

All Articles