2010-11-03 57 views
0

我在我的頁面上有一個$ _REQUEST變量跳轉菜單,允許人們通過post_time更改ASC或DESC順序。

問題在於將DESC命令的默認值更改爲默認值,並使其工作而不更改URL $ _REQUEST變量。

以下是我有:

跳轉菜單:

<form name="form" id="form"> 

<select name="jumpMenu" id="jumpMenu" onchange="MM_jumpMenu('parent',this,0)"> 
<option value ="?post_time=DESC" 
<?php echo ($_REQUEST['post_time']=='DESC')?"selected":"";?> >DESC</option> 
<option value ="?post_time=ASC" 
<?php echo ($_REQUEST['post_time']=='ASC')?"selected":"";?> >ASC</option> 
</select> 

</form> 

ORDER BY子句:

ORDER BY post_time {$_REQUEST['post_time']} 

就像我說的,如果你打開網頁的偉大工程,但它在ASC默認,我試圖將變量設置爲=「DESC」在頂部,它的工作原理,但它不會改變爲ASC。

任何快速意見?

在此先感謝!

回答

0

你試過:

<form name="form" id="form"> 

<select name="post_time" id="jumpMenu" onchange="MM_jumpMenu('parent',this,0)"> 
<option value ="DESC" 
<?php if(($_REQUEST['post_time']=='DESC')||(!isset($_REQUEST['post_time']))){echo"'selected'";}?> >DESC</option> 
<option value ="ASC" 
<?php if($_REQUEST['post_time']=='ASC'){echo"'selected'";}?> >ASC</option> 
</select> 

</form> 
+0

因此它設置如果以前選擇DESC,或者如果沒有先前已選定(默認)。還要注意,我已經稍微調整了表單,以便它能夠與post和get進行正確的匹配,並且在選擇 – SW4 2010-11-03 13:56:53

+0

之前/之後應該添加任何其他字段。我需要添加上面的變量嗎? – eberswine 2010-11-03 14:50:53

0

嘗試

<option value ="DESC" 
<?php if(isset($_REQUEST['post_time']) && $_REQUEST['post_time']=='DESC') { print " selected=\"selected\"";}?> >DESC</option> 
<option value ="ASC" 
<?php if(isset($_REQUEST['post_time']) && $_REQUEST['post_time']=='ASC') { print " selected=\"selected\"";}?> >ASC</option> 

我也勸你不要這樣:

ORDER BY post_time {$ _REQUEST [ 'post_time']}

Try:

$order = 'ASC'; // Default 

// Check for DESC as ASC is the default 
if(isset($_REQUEST['post_time']) && $_REQUEST['post_time'] == 'DESC') { 
    $order = 'DESC'; 
} 

ORDER BY post_time {$order} 

我覺得這個頁面顯示你要完成的一個很好的例子:

​​3210

+0

我正在靠牆碰撞我的頭。對不起。 – eberswine 2010-11-03 14:44:30

+0

if(isset($ _REQUEST ['post_time'])&& $ _REQUEST ['post_time'] =='ASC'){ $ order ='ASC'; } else { \t $ order ='DESC'; \t} [/ code] 但是,當我將跳轉菜單更改爲ASC時,它跳回到DESC? – eberswine 2010-11-03 14:45:22

+0

添加鏈接:http://www.w3schools.com/PHP/php_ajax_database.asp – 2010-11-03 17:07:51

0

永遠不要使用$ _REQUEST。這是一個牀的做法。而

ORDER BY post_time {$ _REQUEST [ 'post_time']}

是不是很好無論是。不要忘記用mysql_real_escape_string轉義所有用戶輸入的數據。

2項以前的建議都相當不錯,沒有什麼要補充