我有一个月的下拉列表,#frm
。
以下代码正在填写下拉列表...
<?php
$from_months = array(0 => 'January', 1 => 'February', 2 => 'March', 3 => 'April', 4 => 'May', 5 => 'June', 6 => 'July', 7 => 'August', 8 => 'September', 9 => 'October', 10 => 'November', 11 => 'December');
$transposed = array_slice($from_months, date('n'), 12, true) + array_slice($from_months, 0, date('n'), true);
$last8 = array_reverse(array_slice($transposed, -8, 12, true), true);
?>
<select class="span4" name="frm" id="frm" required>
<?php
foreach ($from_months as $num => $name) {
printf('<option value="%u">%s</option>', $num, $name);
}
?>
现在我想根据数据库表中的值填充下拉列表(#frm
)。就像...如果数据库表值为4,那么dorpdown(#frm
)将从4 => 'May', 5 => 'June',......
开始显示。
我该怎么办?
答案 0 :(得分:0)
好吧,你只需要调整你的循环。
$value_from_database = 4;
foreach ($from_months as $num => $name)
if ($num >= $value_from_database)
printf('<option value="%u">%s</option>', $num, $name);
答案 1 :(得分:-1)
使用转置数组的逻辑,这样做很简单
<?php
$db_val=6; // the value you get from your database
$from_months = array(0 => 'January', 1 => 'February', 2 => 'March', 3 => 'April', 4 => 'May', 5 => 'June', 6 => 'July', 7 => 'August', 8 => 'September', 9 => 'October', 10 => 'November', 11 => 'December');
$transposed = array_slice($from_months, $db_val, 12, true);
?>
<select class="span4" name="frm" id="frm" required>
<?php
foreach ($transposed as $num => $name) {
printf('<option value="%u">%s</option>', $num, $name);
}
RESULT
<select class="span4" name="frm" id="frm" required>
<option value="6">July</option>
<option value="7">August</option>
<option value="8">September</option>
<option value="9">October</option>
<option value="10">November</option>
<option value="11">December</option>