I need to display select box with decimal numbers by using php FOR loop. See below code:
<select name="animationDelayTime"> <option value="0.1">0.1s</option> <option value="0.2">0.2s</option> <option value="0.3">0.3s</option> <option value="0.4">0.4s</option> <option value="0.5">0.5s</option> <option value="0.6">0.6s</option> <option value="0.7">0.7s</option> <option value="0.8">0.8s</option> <option value="0.9">0.9s</option> <option value="1.0">1.0s</option> <option value="1.1">1.1s</option> <option value="1.2">1.2s</option> <option value="1.3">1.3s</option> </select> This loop would continue until it reach at number 10.
Is it possible to loop decimal numbers in php ?
If possible can anyone check following code that is not working. I fetch data from database and I used conditional statement for selecting and show existing record selected in select box:
<select class="form-control" name="delayTime" title="Choose animation delay time..." data-toggle="tooltip" required> <?php for ($x = 0.1; $x <= 10.1; $x=$x+0.1) { if($x == $updateEntry->delayTime){ echo '<option value="' .$x.'" selected>'.$x.'s</option>'; } echo '<option value="' .$x.'">'.$x.'s</option>'.PHP_EOL; } ?> </select>Please see below screen shot. The value 7.1 should be selected in the select box but it not selected only beginning two or three number get selected, don't know why? Please help me to sort it.
Finally I found the method to solve this issue by using range in foreach loop. See below:
<?php foreach (range(0.1, 10, 0.1) as $val) { $selected = ($val == $updateEntry->delayTime) ? "selected": ""; echo '<option value="' . $val . '" '.$selected.'>' . $val . 's</option>'; } ?> 