[英]How can i make to php write only once selected to the list?
Im going to make a html list with php what is the default value is the current day,month or year.我要用 php 制作一个 html 列表,默认值是当前的日期、月份或年份。
like this:像这样:
Here is the code:这是代码:
while ($start <= $end) {
if ($start == date("Y") || $start == date("d") || $start == date("m")) {
echo "<option selected value='$name'>$start</option> <br>";
} else {
echo "<option value='$name'>$start</option> <br>";
}
$start++;
}
echo "</select>";
The problem is the logic is not working fine, because at the day or month cant be separated and it always make 2 selected item in html, and and always applies the last one.问题是逻辑不能正常工作,因为在日期或月份不能分开,它总是在 html 中生成 2 个选定的项目,并且总是应用最后一个。
Anyone can help me to solve this problem?任何人都可以帮我解决这个问题吗? Any tips, maybe this is not the good way for this.任何提示,也许这不是解决此问题的好方法。
Set a variable to compare with depending on which dropdown you're creating, which is appareently in the $name
variable.根据您正在创建的下拉列表设置一个变量进行比较,这显然在$name
变量中。 Then you won't mistakely select the Day value that matches the current month, or vice vera.这样您就不会错误地选择与当前月份匹配的 Day 值,反之亦然。
if ($name == 'Year') {
$selected = date('Y');
} elseif ($name == 'Month') {
$selected = date('m');
} elseif ($name == 'Day') {
$selected = date('d');
}
while ($start <= $end) {
if ($start == $selected) {
echo "<option selected value='$start'>$start</option> <br>";
} else {
echo "<option value='$start'>$start</option> <br>";
}
$start++;
}
echo "</select>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.