1

I have a search box which a user can search their favorite cases. I have ordered the search results by date, but I want to make it optional for a user and they can choose it by themselves:

$orders  = array("date","price"); //field names
$key     = array_search($_GET['sort'],$orders)); // see if we have such a name
$orderby = $orders[$key]; //if not, first one will be set automatically. smart enuf :)
    $quer = "SELECT*FROM ".$db_table." WHERE  
     `case`=
    'apartment'
    AND `field`=
    'sell'
     ORDER BY 
     $orderby";
    $query=mysqli_query($connect,$quer)
    or die(mysqli_error());
    ?>

    <?php while($row = mysqli_fetch_array($query)):
    echo "price ";
    echo "address ";
    //to simplify the code I do not write the rest
    ?>
    <?php endwhile;?>

For example, order by date or price or other things. How can I do it?

Users can get values from dropdownlist:

<select>
    <option value="date">order by date</option>
    <option value="price">order by price</option>
</select> 
Gynteniuxas
  • 7,035
  • 18
  • 38
  • 54
Malekian
  • 315
  • 8
  • 27
  • http://stackoverflow.com/a/8255054/285587 – Your Common Sense Jun 16 '16 at 10:41
  • i edit my code but still i can not get result. and i have a question i do not know what is 'sort' in this code and i have to replace it with other variable or not? – Malekian Jun 16 '16 at 15:55
  • I would like to get some more information. Does user click on button (submits form)? If yes, then what fields are in there and if it's possible for user to order by however he wants? – Gynteniuxas Jun 16 '16 at 18:18
  • thank you for your answering. no it is a drop down list and user can select one of the options in the list. – Malekian Jun 16 '16 at 18:56

1 Answers1

2

Here one example of have you can get this by allowing user to choose order by what and even in whether ASC/DESC (this is also immune to MySQL injections because of set code values). You can remove checkbox if not needed but then don't forget to remove that part in query too:

if (!empty($_POST['dropdownOption']))
{
    $orderBy = ($_POST['orderValue'] == "date") ? "date" : "price";
    $orderType = (!empty($_POST['orderType'])) ? "DESC" : "ASC";
    $quer = "SELECT * FROM TABLE WHERE `case` = 'apartment' AND `field` = 'sell' ORDER BY ".$orderBy." ".$orderType."";
    $query = mysqli_query($connect, $quer) or die(mysqli_error());
}

?>

<form method="post" action="">
    <select name="orderValue">
        <option value="date">order by date</option>
        <option value="price">order by price</option>
    </select><br>
    <input type="checkbox" name="orderType" value="1">In descending order?</input><br>
    <input type="submit" name="dropdownOption" value="Apply">
</form>

Note that I gave this as a form (because I'm not informed how your website looks like and I don't know if you're Ajax or something else. If you're Ajax, then you need to make small changes, actually). With the help of CSS, you can achieve this as if it is just a select menu.

Gynteniuxas
  • 7,035
  • 18
  • 38
  • 54
  • thank you vary much it worked for me. just one question in this case, website does not show any result until user select one option what can i do that result show for example based on date by default and if user wants to change it he or she can change results order – Malekian Jun 16 '16 at 20:21
  • and i noticed now is not there any way we can omit submit button because in most of the sites after you click on a option in drop down list results began to change and they do not use submit button in this situations. thank you – Malekian Jun 16 '16 at 20:29
  • i solved my first question by deleting if now the results shown based on price by default . but till now i can not solve the second question. – Malekian Jun 16 '16 at 20:46
  • 1
    second problem solved too. we have to write
    in this case after click on a option it submit new value
    – Malekian Jun 16 '16 at 20:56
  • Well, I wasn't during that time you wrote these but yes, that Javascript would work well. – Gynteniuxas Jun 16 '16 at 21:23