Order by using selected values from drop down list without using form

Searched many options to find some easy solution where i can sort my results but din’t find any.
I want to order the displayed results by selected drop down values. I don’t want to use “form”. Want the other way to sort it.

<div class="col-lg-2">
<label class="margin-bottom:25px;" style="margin-left:75px;"> Sort by: <label>
</div>
<div class="col-lg-2">
    <select class="form-control" id="sortby" name="sortby">
        <option selected value="ID">ID</option>
        <option value="Relevance">Name</option>
        <option value="Name">Source</option>
    </select>
</div>

The above is my drop down list.
The below is our $sql initial query :

$condition = implode(' AND ', $query);
$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source,candidate.cand_req,candidate_contact.cand_location FROM candidate ".$join.' where '.$condition;

Now we have tried to this so far ,but it seems something is wrong.

if($_POST['sortby']=="ID")
{
 $sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source FROM candidate ".$join.' where '.$condition."order by candidate.cand_number" ;
}

if($_POST['sortby']=="Name")
{
 $sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source FROM candidate ".$join.' where '.$condition."order by candidate.cand_fname" ;
}

if($_POST['sortby']=="Source")
{
 $sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_source FROM candidate ".$join.' where '.$condition."order by candidate.cand_Source" ;
}

What is wrong as this is not working for me ? I dont want to use form. Suggest me some easy solution. Thank You


Source: mysql

Leave a Reply