根据用户选择排序我的查询

I am trying to sort the price of the products depending on the user choose as (lowest price) and (highest price)

here is my HTML code

<li class="item2"><a href="#">Price <i></i></a>
                    <ul style="display: none;">
                        <li>
                            <a href="#"><label for="lowest">
                                    <input type="checkbox" name="lowest" id="lowest" value="lowest">
                                    Lowest</label>
                            </a>
                        </li>
                        <li>
                            <a href="#"><label for="new">
                                    <input type="checkbox" name="new" id="new" value="new"> Highest</label>
                            </a>
                        </li>
                    </ul>
                </li> 

and here is the my PHP code

//filtration conditions
            if (isset($_GET['fp'])) {
                $fp = $_GET['fp'];
                if ($fp == "l") {
                    $fpw = 'ORDER BY pPrice DESC';
                } elseif ($fp == "h") {
                    $fpw = 'ORDER BY pPrice ASC';
                }
            } else {
                $fp = '';
            }

            $getData = $db->prepare('SELECT id, pName, pImage, pPrice, alt FROM products
            WHERE cat=? AND subCat=? ORDER BY id DESC LIMIT ?,?');
            $getData->bind_param('iiss', $cat, $subCat, $startFrom, $perPage);
            $getData->execute();
            $getData->store_result();
            $getData->bind_result($id, $pName, $pImage, $pPrice, $alt);
            while ($getData->fetch()) {
                ?>
                <div class="grid_1_of_4 images_1_of_4">
                    <div class="quickViewContainer">
                        <img src="../images/productsMainImages/<?php print $pImage ?>" alt="<?php print $alt ?>">
                        <span><a href="?p=3&amp;pd=2&amp;proId=<?php print $id ?>"><i class="fa fa-eye"></i> Quick View</a></span>
                    </div>
                    <h2><?php print $pName ?> </h2>
                    <p><span class="price"><?php print $pPrice ?> LE.</span></p>
                    <div class="button">
                        <span class="pull-right wishlist2"><a href="#" title="Add to Wishlist"><i
                                    class="fa fa-heart hvr-pulse"></i></a></span>
                    </div>
                </div>
                <?php
            }
            ?>

how can I change the ORDER BY dynamically

I will give you very high level idea how to do this.

You have a dropdown. You can have onchange event. When user change a value, it will be triggered. Get the value and make a call using ajax or direclty hit the url with query strings.

If you use ajax, refer ajax. You can pass the selected value through ajax POST. Get the value on your php side and concatenate it to the query.

If you use query string way, just add it as follows.

    window.location.href=`process.php?val=userSelectedValue`

In the proces.php page, get the value and concatenate it with the query.

TO concatenate just use as follows

          $query='SELECT id, pName, pImage, pPrice, alt FROM products
        WHERE cat=? AND subCat=? ORDER BY'. $userSelectedVal .' DESC LIMIT ?,?'

First of all change choice checkbox to radio and set column name as value in radio get radio value on server and use in and pass in query to sort data.

To prevent sql injection make an array of columns in php and validate submitted radio value with the array like this:

/* columns can be used to sort */
$columns = array('col1', 'col2');
$sortBy = $_GET['sortBy'];


if(!in_array($sortBy, $columns)){
  /* invalid column name provided set to default  */
  $sortBy = 'defaultColumn';
}

$query = 'SELECT id, pName, pImage, pPrice, alt FROM products WHERE cat = ? AND subCat = ? ORDER BY '. $sortBy . ' DESC LIMIT ?, ?';