2012-05-08 81 views
1

在下拉框中的起始行值我有使用日期表:設置在PHP

ID,的DateField,dayfield,monthyearfield

1,2012-05-01,星期二,五月-2012

2,2012-05-02,週三,5月2012

我使用的是dropdownbox,我選擇DISTINCT monthyearfield。例如,如果該月份是2012年6月,我希望下拉框默認爲頁面打開時。我不知道該怎麼做。如果我使用WHERE子句,它只會顯示2012年6月的情況。目前它默認爲2012年5月的第一行。我嘗試了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>"; 
?> 

回答

0
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>"; 
} 
0

只是這一行:

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

如果($ ROW2 [ 'monthyearfield'] == '君-2012') $下拉= 「選定=選擇」。

$ dropdown。='/ option>';

1

您可以使用PHP date[docs]功能 並添加如果要檢查它。 喜歡:

$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>"; 
} 
0

你想訂購您的結果,或者你想在你的選項列表中選擇全自動的特定項目?我建議這是最後一個,不是嗎? 。

所以,帶來的 「」 -tag,其中包含應全自動選擇的值時,「選擇= '選擇' atttribute /值

FE:

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

所以該項目標題「Foo」將自動選中。