如何使用GET和POST变量进行排序和搜索?

时间:2009-11-29 03:24:45

标签: php search sorting post get

我目前正在使用列标题作为链接,点击后将通过向网址添加get变量按列名对结果进行排序。这是一个例子:

<a href="
  <?php
  // Sorts by order id. If already sorted by order id, then it will change the link to sort descending
  if(!isset($_GET['sortby']) || $_GET['sortby'] != 'order_id'){
    echo $_SERVER['SCRIPT_NAME'] . '?sortby=order_id';  //example: tracker.php?sortby=order_id
  } elseif(isset($_GET['sortby']) || $_GET['sortby'] == 'order_id'){
    echo $_SERVER['SCRIPT_NAME'] . '?sortby=order_id_desc'; //example: tracker.php?sortby=order_id_desc
  }?>
">Order ID</a>

我还有一个表单,用户可以从选择框中输入一个类别,然后输入一个searchterm。我使用if语句和switch语句检查是否设置了$ _GET ['sortby']变量和$ _POST ['search_submit']变量,如果是,则根据GET变量的值运行某个sql语句

有4种不同的场景。

1。默认值:如果既未设置排序也未设置搜索。这个工作正常:

if(!isset($_GET['sortby']) && !isset($_POST['search_submit'])){ //Default, If no sort or search is set
  $sql = 'SELECT * 
          FROM orders 
          ORDER BY order_id DESC'; 
}

2。如果设置了搜索但排序不是。这个工作正常:

if(isset($_POST['search_submit'])) {
  $search_string = ' WHERE ' . $_POST['searchby'] . '= "' . $_POST['search_input'] . '" ';
}

if(!isset($_GET['sortby']) && isset($_POST['search_submit']) ){ //If the search is set but no sort
  $sql = "SELECT * 
          FROM orders"
          . $search_string . 
          "ORDER BY order_id DESC";
}

第3。如果设置了排序,但搜索不是。这个工作正常:

if(isset($_GET['sortby']) && !isset($_POST['search_submit'])) { //If the sort is set but no search
  switch ($_GET['sortby']) { 
    case "order_id":
      $sql = "SELECT * 
              FROM orders 
              ORDER BY order_id ASC";
    break;

    case "order_id_desc":
      $sql = "SELECT * 
              FROM orders  
              ORDER BY order_id DESC";
    break;
  }
}

4。如果设置了搜索AND排序。以上所有3个if语句都有效,但最后一个给我带来了问题。

if(isset($_GET['sortby']) && isset($_POST['search_submit'])) { //If the sort AND search is set
  switch ($_GET['sortby']) { 
    case "order_id":
      $sql = "SELECT * 
              FROM orders"
              . $search_string . 
              "ORDER BY order_id ASC";
    break;

    case "order_id_desc":
      $sql = "SELECT * 
              FROM orders"
              . $search_string . 
              "ORDER BY order_id DESC";
    break; 
  }
} 

您可以搜索,但是只要您单击其中一个列标题并使用新的GET变量重新加载页面,它就会删除当前的POST变量,从而再次显示所有结果。我尝试在$ _POST ['search_submit'] isset之后将当前POST变量加载到会话中,然后进行最后一次if语句检查以查看会话变量是否已设置,但接下来会发生的是会话始终设置并且如果我尝试回到主页,它将保留这些搜索结果。

也许我需要在某处摧毁会话?也许我可以采用整体更好的方法来组合排序和搜索功能?

4 个答案:

答案 0 :(得分:3)

我建议您将搜索表单从method="POST"更改为method="GET",并仅使用GET来处理所有请求。如果您无法更改POST请求,则需要POST每个请求(包括排序),这将需要javascript附加到您的排序链接。

使用GET的好处是您的用户可以为特定搜索添加书签,因为所有数据都将包含在查询字符串中。

编辑:在后续请求中保留搜索字符串:

我会把你的排序代码抽象成这样的东西:

<?php
function write_sortable_header_link( $column_id, $column_name ){
   if( ( isset($_GET['sortby']) && $_GET['sortby'] != $column_id ) || !isset($_GET['sortby']) )
     $query = "?sortby=$column_id";
   else
     $query = '?sortby='.$column_id.'_desc';

   if( isset($_GET['searchsubmit']) ){
     $query .= '&amp;searchsubmit=1';
     $query .= '&amp;searchby=' . urlencode( isset($_GET['searchby']) ? $_GET['searchby'] : '' );
     $query .= '&amp;search_input=' . urlencode( isset($_GET['search_input']) ? $_GET['search_input'] : '' );
   }

   $href = $_SERVER['SCRIPT_NAME'] . $query;
   echo "<a href='$href'>$column_name</a>";
}
?>

然后你会这样称呼它:

<?php write_sortable_header_link( 'order_id', 'Order Id' ); ?>

它将确保您的排序URL包含持久性的正确查询字符串参数。

答案 1 :(得分:2)

尝试仅使用$_GET,似乎不需要$_POST

答案 2 :(得分:1)

不是你问题的答案,而只是我的0.2

在您的情况下,我通常使用javascript在Web浏览器中进行排序客户端。它仅使用不同的ORDER BY参数来防止基本上相同的查询一次又一次地运行。

使用jquery甚至有一些非常好的插件可以让它变得非常简单。

示例:http://tablesorter.com/docs/

答案 3 :(得分:1)

这是我最终用于使用dcneiner建议的排序和搜索获取变量来重写链接的代码。我拿出了urlencode,切换后的&amp;到'&amp;'签名并使内联if语句只读取get变量,因为这些get变量可以设置的唯一方法是设置search_submit,因为它们是同一个表单的一部分。我还将'{'和'}'添加回if和else语句中。我猜你在使用一种稍微不同的PHP方式?你看到我所做的改变有什么不对或不安全吗?我不太确定你为什么这样做。但再次感谢。

function write_sortable_header_link( $column_id, $column_name ){ //Function that creates a link with the search query if needed
  if( ($_GET['sortby'] != $column_id) || !isset($_GET['sortby']) ) { //If the GET variable is not the column id of this button or if the GET sortby variable has not been set
    $query = "?sortby=$column_id"; //then add this to the end of the url
  } else {
      $query = '?sortby='.$column_id.'_desc'; //otherwise if the GET variable is the column id of this button, then add the descending code to the end of the variable
  }

  if(isset($_GET['search_submit']) ){ //If the GET variable search_submit is in the url
    $query .= '&search_submit=1'; //then add this to the end of the url string
    $query .= '&searchby=' . $_GET['searchby']; //add whatever is currently in the GET searchby to the end of the url string
    $query .= '&search_input=' . $_GET['search_input']; //add whatever is currently in the GET search_input to the end of the url string
  }

  $href = $_SERVER['SCRIPT_NAME'] . $query; //this is the href part of the link
  echo "<a href='$href'>$column_name</a>"; //this creates the actual link
}