1

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. 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>'; } ?> 
1
  • 1
    For such a question, you should share what you've tried and what exactly is not working Commented Nov 11, 2018 at 13:29

4 Answers 4

4

You can do this by many ways. For example using a for() loop. Start with 0.1, go till it is 10 and increment on every step by 0.1

range() is also a good choice for this but then you've to use foreach()

<?php echo '<select name="animationDelayTime">'; for ($x = 0.1; $x <= 10; $x=$x+0.1) { echo '<option value="' .$x.'">'.$x.'s</option>'.PHP_EOL; } echo '</select>'; ?> 

DEMO: https://3v4l.org/RlAcM

EDIT: AS per new requirement change. Here I just assume the value of $updateEntry->delayTime is 0.5. You can use it as you're getting it from DB.

<select class="form-control" name="delayTime" title="Choose animation delay time..." data-toggle="tooltip" required> <?php //$selected = ''; for ($x = 0.1; $x <= 10.1; $x=$x+0.1) { $selected = ($x == 0.5) ? "selected": ""; echo '<option value="' .$x.'" '.$selected.'>'.$x.'s</option>'.PHP_EOL; } ?> </select> 
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for all your help.
Can you please check my question again, I'm getting a very strange issue.
@PawanMall I just assume the value of $updateEntry->delayTime is 0.5 then it is working as expected and for 0.5 the option is selected See demo 3v4l.org/iLRIW You just need to do some tweak. Also I've edited my answer as per your new requirement. Let me know is that you want or not?
I check your code but getting same issue If i change the value, 3v4l.org/6CqbW
Finally I resolve the issue by using range in foreach loop and thanks for all your support. Much appreciated :)
|
4

Simple with range:

// first `0.1` - start value, `10` - end value, second `0.1` - step foreach (range(0.1, 10, 0.1) as $val) { echo '<option value="' . $val . '">' . $val . 's</option>'; } 

2 Comments

thanks buddy for all your help. I got what I want to do.
I found this answer to be better. I do not know why but when I set my values using the accepted answer as ($x = 89.0; $x <= 108.0; $x=$x+0.1) I got many lines echoing correctly but several returning as "94.89999999999" etc.
2

You can use While loop:

// Initialize the variable $val = 0.1; // Loop until the value is less than or equal to 10 while ($val <= 10) { // generate the string using $val echo '<option value="' . $val . '">' . $val . 's</option>'; // Increment the value by 0.1 for next iteration $val += 0.1; } 

Comments

0

Try this

For( $x = 0 ; $x < 1.4; $x += 0.1){ echo "<option value='$x'>".$x."s</option>"; } 

1 Comment

Incrementing by 0.1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.