doulun9534 2012-05-08 08:05
浏览 46
已采纳

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

I've got a table with dates:

id,datefield,dayfield,monthyearfield

1,2012-05-01,Tuesday,May-2012

2,2012-05-02,Wednesday,May-2012

I'm using a dropdownbox where I select DISTINCT monthyearfield. If the month is for instance Jun-2012 I want the dropdownbox to default to that when the page opens. I don't know how to do that. If I use a WHERE clause it only shows for instance Jun-2012. Currently it is defaulting to the first row, which is May-2012. I tried ORDER BY variable ($date_start1) but that is not working.

<?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 .= "
<option value='{$row2['id']}'>{$row2['monthyearfield']}</option>";
}
$dropdown .= "
</select>";
echo $dropdown;
echo "<input type=submit name=Submit value='Go'>";
$data1 = mysql_real_escape_string($_POST["myvalue"]);
echo "</form>";
?>
  • 写回答

4条回答 默认 最新

  • dongqiulei6805 2012-05-08 08:13
    关注
    while ($row2 = mysql_fetch_assoc($res2)) {
        $my = $row2['monthyearfield'];
        $selected = "";
        if ( $my == $date_start1 ) {
          $selected = " selected='selected'";
        }
        $dropdown .= "
    <option value='{$row2['id']}'{$selected}>{$row2['monthyearfield']}</option>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 MIT控制器能控制不稳定系统吗
  • ¥15 公司代码X对业务伙伴X无效,处理方法?
  • ¥15 微信内链接跳转到浏览器打开怎么实现
  • ¥15 三角波可以直接加施密特电路整形到矩形波吗实物
  • ¥15 html,php,在使用html请求php文件时发生了错误,无法请求到php文件读取数据库并用javascript进行数据显示,刷新
  • ¥15 touchsocket udp组播
  • ¥20 MAC怎么安装Silverlight 插件?以及安装了怎么启用
  • ¥15 VS2012中查询语句无法填入解析,数值传不进去
  • ¥15 gis系统开发出现命名空间“ESRI.ArcGIS”中不存在类型或命名空间名“Analyst3D”报错
  • ¥15 怎么让ai定时给我发信息 c#或者python