开发者

Dynamically create in PHP a drop-down list from a range of numbers *with increments*

开发者 https://www.devze.com 2022-12-17 02:10 出处:网络
I need to dynamically create in PHP a drop-down list of numerical choices 开发者_如何学JAVAlike so:

I need to dynamically create in PHP a drop-down list of numerical choices 开发者_如何学JAVAlike so:

<select>
<option value="120">120 cm</option>
<option value="121">121 cm</option>
<option value="122">122 cm</option>
<option value="123">123 cm</option>
<option value="etc...
</select>

I would like to only specify the starting and ending numbers.

Thanks for any help.


echo "<select>";
$range = range(120,130);
foreach ($range as $cm) {
  echo "<option value='$cm'>$cm cm</option>";
}
echo "</select>";

The range() function can handle all of the situations you described in the comment.

range(30.5, 50.5, 0.5); // 30.5, 31, 31.5, 32, etc

range(30, 50, 2); // 30, 32, 34, 36, 38, 40 etc


Also:

echo "<select>";
for ($cm = 120; $cm <= 130; $cm++) echo "<option value='$cm'>$cm cm</option>";
echo "</select>";

0

精彩评论

暂无评论...
验证码 换一张
取 消