GET変数とPOST変数の両方を使用してソートと検索を行うにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1814544

質問

現在、列ヘッダーをリンクとして使用しています。リンクをクリックすると、URLに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>

ユーザーが選択ボックスからカテゴリを選択して入力し、検索語を入力できるフォームもあります。 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ステートメントはすべて機能しますが、最後の1つは問題を引き起こしています。

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ステートメントをチェックしようとしましたが、その後、セッションは常に設定され、ホームページに戻ると、それらの検索結果が保持されます。

おそらくどこかでセッションを破棄する必要がありますか?おそらく、並べ替え機能と検索機能を組み合わせるために採用できる全体的に優れたアプローチがありますか?

役に立ちましたか?

解決

検索フォームを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に永続化のための正しいクエリ文字列引数が含まれていることを確認します。

他のヒント

$_GETのみを使用して、$_POSTを使用する必要はないようです。

あなたの質問への答えではなく、ちょうど私の0.2

あなたの状況では、私は通常、JavaScriptを使用してWebブラウザでクライアント側のソートを行います。異なるORDER BYパラメーターのみを使用して、本質的に同じクエリが繰り返し実行されるのを防ぎます。

jqueryには、非常に簡単にする非常に優れたプラグインさえあります。

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

これは、dcneinerによって提案されたソートおよび検索取得変数を使用してリンクを書き換えるために使用したコードです。私はurlencodeを取り出し、スイッチされた<!> amp; 「<!> amp;」これらのget変数を設定できる唯一の方法は、同じフォームの一部であるためsearch_submitが設定されている場合のみであるため、インラインifステートメントをget変数としてのみ読み取るようにします。また、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
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top