简体   繁体   English

我怎样才能让 php 只写一次选择到列表中?

[英]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 个选定的项目,并且总是应用最后一个。

htmllius

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.

相关问题 如何才能使此Php WP_Query返回类别仅一次? - How can I make this Php WP_Query returning categories only once? 如何在PHP中只为绝对路径定义一次常量? - How can I define the constant for absolute path only once in PHP? 我怎样才能在foreach循环PHP中仅回显一次 - How can i echo only once inside a foreach loop PHP 将链接重定向到我网站上的随机页面的 php 函数只能工作一次。 我怎样才能让它每次都工作? - php function that redirects a link to a random page on my website only works once. How can I make it work every time? PHP正在鼠标在窗口上移动时执行JavaScript。 它在一个函数上循环。 如何使它仅执行一次? - PHP is executing a javascript on mouse move over window. It's looping on a function. How can I make it execute only once? 如何从选项中创建变量:在 PHP $_POST 中选择? - How can I make variables from option:selected in PHP $_POST? 如何仅在字符串 PHP 中获取选定的数字 - How can I get selected number only in string PHP 我如何在php中获取下拉列表的选定值? - how can i get the selected value of dropdown list in php? 如何使PHP自动写入文件? - How can I make the PHP write to the file automaticly? 我正在制作一个PHP测验应用程序。 如何确保只问一次问题? - I am making a PHP quiz app. How do I make sure the questions are asked only once?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM