简体   繁体   English

在php的下拉框中设置起始行值

[英]set the starting row value in dropdown box in php

I've got a table with dates: 我有一张桌子,上面有日期:

id,datefield,dayfield,monthyearfield id,datefield,dayfield,monthyearfield

1,2012-05-01,Tuesday,May-2012 2012年5月1日,2012年5月2日

2,2012-05-02,Wednesday,May-2012 2012年5月2日,星期三,2012年5月

I'm using a dropdownbox where I select DISTINCT monthyearfield. 我正在使用一个下拉菜单,在其中选择DISTINCT monthyearfield。 If the month is for instance Jun-2012 I want the dropdownbox to default to that when the page opens. 例如,如果月份为2012年6月,则我希望下拉框默认为页面打开时的下拉框。 I don't know how to do that. 我不知道该怎么做。 If I use a WHERE clause it only shows for instance Jun-2012. 如果我使用WHERE子句,则仅显示例如2012年6月。 Currently it is defaulting to the first row, which is May-2012. 当前,它默认为第一行,即2012年5月。 I tried ORDER BY variable ($date_start1) but that is not working. 我尝试了ORDER BY变量($ date_start1),但这不起作用。

<?php
$date_start1 = "Jun-2012"; //just for testing purposes, will change later so that it gets the current Month.
echo $date_start1;
$sql2 = "SELECT DISTINCT monthyearfield FROM trn_cal";
$res2 = mysql_query($sql2);
echo "<form method='post'>";
$dropdown = "<select name='myvalue'>";
while ($row2 = mysql_fetch_assoc($res2)) {
    $my = $row2['monthyearfield'];
    $dropdown .= "\r\n<option value='{$row2['id']}'>{$row2['monthyearfield']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
echo "<input type=submit name=Submit value='Go'>";
$data1 = mysql_real_escape_string($_POST["myvalue"]);
echo "</form>";
?>

you can use php date [docs] function and add a if to check it. 您可以使用php date [docs]函数并添加if进行检查。 like: 喜欢:

$cur_date = date('M-Y');
while ($row2 = mysql_fetch_assoc($res2)) {       
    $selected = "";
    if ($row2['monthyearfield'] == $cur_date) {
       $selected = "selected='selected'";
    }
    $dropdown .= "\r\n<option value='{$row2['id']}' {$selected}>{$row2['monthyearfield']}</option>";
}

just edit this line: 只需编辑此行:

$dropdown .= "\r\n<option value='{$row2['id']}'>{$row2['monthyearfield']}";

if ($row2['monthyearfield']== 'Jun-2012') $dropdown .="selected=selected"; 如果($ row2 ['monthyearfield'] =='2012年6月')$ dropdown。=“ selected = selected”;

$dropdown .=' /option> '; $ dropdown。='/ option>';

while ($row2 = mysql_fetch_assoc($res2)) {
    $my = $row2['monthyearfield'];
    $selected = "";
    if ( $my == $date_start1 ) {
      $selected = " selected='selected'";
    }
    $dropdown .= "\r\n<option value='{$row2['id']}'{$selected}>{$row2['monthyearfield']}</option>";
}

do you want to order your results, or do you want to automaticly select a specific item in your option-list? 您要订购结果还是要在选项列表中自动选择特定项目? i suggest it is the last one, or isn't it? 我建议这是最后一个,不是吗?

so, give the ""-tag, which contains the value which should be automaticly selected, the "selected='selected' atttribute/value. 因此,给出“”标记,其中包含应自动选择的值,即“ selected ='selected”属性/值。

fe: FE:

<select>
    <option>Bar</option>
    <option selected="selected">Foo</option>
    <option>FooBar</option>
</select>

So the item with the title "Foo" will be automaticly selected. 因此,将自动选择标题为“ Foo”的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM