开发者

Order by MYSQL dynamic clause. Using $_REQUEST

I have a $_REQUEST variable jump menu on my page allowing people to change the ASC or DESC order by post_time.

The problem is getting the default to DESC order and having it work without changing the URL $_REQUEST variable.

Here is what I have:

jump menu:

<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 clause:

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

Like I said, it works great if you open the page, but it defaults at ASC, I tried to set the variable to = "DESC" at the top and it works, but then it won't change to ASC.

Any quick ideas?

Thanks in advance!!


Have you tried:

<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>


Never ever use $_REQUEST. It's a bed practice. And

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

is not very nice either. Don't forget escape all user entered data with mysql_real_escape_string.

2 previous advices are quite good, have nothing to add


try

<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>

I also wouldn't advise you doing this:

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}

I think this page shows a good example of what you're trying to accomplish:

http://www.w3schools.com/PHP/php_ajax_database.asp

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜