title: Order by date / price
body: <p>I want to show database records ordered by Datetime or Price when users click on links sort by Datetime or sort by Price. Should I add a condition into the query ?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code> $sql = "SELECT p.title AS Title, p.date_published) AS Datetime , p.price   AS Price, c.name AS Category FROM products p
  
             INNER JOIN categories c
                ON c.id = p.category
             INNER JOIN brands b
                ON b.id = p.brand
                .
                .
                .
      ";
  
      if (isset($_GET['search'])){
  
            $locations = array();
            $getters = array();
            $queries = array();
  
            foreach($_GET as $key =&gt; $value) {
            .
            .
            .
            }
  
            if (!empty($brands)) {
              $brd_q = implode(",",$brands);
            }
  
            if(!empty($getters)) {
  
              foreach($getters as $key =&gt; $value){
                ${$key} = $value;
                switch($key) {
                  case 'search':
                  array_push($queries, "(p.title LIKE '%$search%' || p.description LIKE '%$search%' || p.number LIKE '%$search%')");
                  break;
                  case 'scategory':
                  array_push($queries, "p.category = $scategory");
                  break;
                  case 'sbrands':
                  array_push($queries, "p_brd.brand_id IN ($brd_q)");
                  break;
                  .
                  .
                  .
                }
             }
          }
           
           if(!empty($queries)) {
               $sql .= " WHERE ";
               $i=1;
               foreach($queries as $query) {
                   if ($i &lt; count($queries)) {
                       $sql .= $query." AND ";
                   }else{
                       $sql .= $query;
                   }
                   $i++;
               }
  
           }
          
            $sql .= " ORDER BY Datetime DESC";
        } </code></pre>
  </div>
  </div>
  </p>