0

我是一名学生编码员。我想创建一个排序按钮,按下该按钮时,按升序对 MYSQL 查询进行排序。然后再次按下时,它将按降序排序。对,现在,它只是背靠背显示升序表和降序表。

形成此按钮逻辑的最佳方法是什么?

  <form name="Email Header" method="post" action="{$_SERVER['PHP_SELF']}">
    <button type="submit" name="id_sort" class="button" value="1">ID</button>
    <button type="submit" name="from_sort" class="button" value="1">FROM</button>
    <button type="submit" name="to_sort" class="button" value="1">TO</button>
    <button type="submit" name="subject_sort" class="button" value="1">SUBJECT</button>
    <button type="submit" name="date_sort" class="button" value="1">DATE</button>
 </form>


// SORT 

if( isset( $_POST['id_sort'] ) )
   if( $_POST['id_sort'] == 1 )
     id_ASC_sort();
     $_POST['id_sort'] = 0;

if ($_POST['id_sort'] == 0)
  if( isset( $_POST['id_sort'] ) )
     if( $_POST['id_sort'] == 1 ) 
        id_DESC_sort();


 // query
    $sql = "SELECT * FROM messages ORDER BY message_id DESC";
    $result = mysql_query( $sql );
4

2 回答 2

2

检查您的第二个条件块。你有一个单等号而不是双等号。这意味着您实际上是在设置变量而不是检查它的值,它总是评估为真。

于 2012-07-28T04:06:09.373 回答
0

我在您的代码中更改了以下行:-

<form name="Email Header" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

并添加了一个按钮:-

<button type="submit" name="asc_sort" id="asc_sort" class="button" value="1">Sort</button>

和变更查询

if(isset($_POST['asc_sort']) && !empty($_POST['asc_sort']) && $_POST['asc_sort']==1)
{
     $sql = "SELECT * FROM messages ORDER BY message_id ASC";

}else{

    $sql = "SELECT * FROM messages ORDER BY message_id DESC";
}

echo $sql;

尝试这个

<form name="Email Header" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<button type="submit" name="id_sort" class="button" value="1">ID</button>
<button type="submit" name="from_sort" class="button" value="1">FROM</button>
<button type="submit" name="to_sort" class="button" value="1">TO</button>
<button type="submit" name="subject_sort" class="button" value="1">SUBJECT</button>
<button type="submit" name="date_sort" class="button" value="1">DATE</button>
<button type="submit" name="asc_sort" id="asc_sort" class="button" value="1">Sort</button>

于 2012-07-28T04:01:37.253 回答