I need to dynamically create in PHP a drop-down list of numerical choices like so:
我需要在PHP中动态创建一个数字选择的下拉列表,如下所示:
<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.
感谢任何帮助。
2 个解决方案
#1
14
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()函数可以处理注释中描述的所有情况。
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
#2
1
Also:
另外:
echo "<select>";
for ($cm = 120; $cm <= 130; $cm++) echo "<option value='$cm'>$cm cm</option>";
echo "</select>";
#1
14
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()函数可以处理注释中描述的所有情况。
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
#2
1
Also:
另外:
echo "<select>";
for ($cm = 120; $cm <= 130; $cm++) echo "<option value='$cm'>$cm cm</option>";
echo "</select>";