if else条件在sqlsrv查询中

I'm assigning a variable to a SQLSRV query that displays the table information in an ORDER BY call. Here's an example:

$fromsite = $dataConnection->SelectAllWhere( "applicants", "loan_purpose='VA Streamline' AND statusdate >= '3/1/2011'", "ORDER BY fromsite DESC" );

I want to take the same variable ($fromsite) and do an "ORDER BY fromsite ASC" keeping all the same query information the same. Can I place this in an "if else" conditional statement? Or, would a switch work better?

Is this what you're looking for?

$fromsite = $dataConnection->SelectAllWhere( 
    "applicants", 
    "loan_purpose='VA Streamline' AND statusdate >= '3/1/2011'", 
    "ORDER BY fromsite " . ( $isAscending ? 'ASC' : 'DESC' ) 
);

I'm assuming you have some value (maybe a $_GET parameter) to decide whether to sort ascending or descending. If $isAscending evaluates to true, the query will sort ascending, otherwise descending.

What I used there is called the ternary operator (?:). You can find it on the PHP comparison operators page (http://php.net/manual/en/language.operators.comparison.php).

EDIT: Assigning $isAscending will differ depending on the input passed in... but here are some examples.

Checkbox - checked is ascending, unchecked is descending

$isAscending = isset( $_REQUEST['input_name'] ) ? true : false;

Checkbox - checked is descending, unchecked is ascending

$isAscending = isset( $_REQUEST['input_name'] ) ? false : true;

Select drop down - values ASC, DESC

$isAscending = $_REQUEST['input_name'] == 'ASC' ? true : false;

Select drop down - 0 is ascending, 1 is descending

$isAscending = $_REQUEST['input_name'] == 0 ? true : false;