John Davidson

Short IF and Long IF Variable Assignment Method in PHP

0 comments
Message:


I have 2 codes that do the same task. 2.In the long code $sort = 'newest'; I'm throwing the variable. But in the 1st shortcode I don't know how to assign it.


*The code to be edited is the 1st Code. (Short Code)


Short IF PHP Code


$sql_code_orderby = ( $sort == 'oldest' ? "ORDER BY Q.created ASC" : ($sort == 'votes' || ($sort == '' && $filter == 'noanswers')) ? "ORDER BY Q.vote_count DESC" : ($sort == 'viewed' ? "ORDER BY Q.viewed DESC" : "ORDER BY Q.created DESC" ) );

Long IF PHP Code


if( $sort == 'oldest' ){
$sql_code_orderby = "ORDER BY Q.created ASC";
} else if( $sort == 'votes' || ($sort == '' && $filter == 'noanswers') ){
$sql_code_orderby = "ORDER BY Q.vote_count DESC";
} else if( $sort == 'viewed' ){
$sql_code_orderby = "ORDER BY Q.viewed DESC";
} else {
$sort = 'newest';
$sql_code_orderby = "ORDER BY Q.created DESC";
}

My fault trial result:


$sql_code_orderby = ( $sort == 'oldest' ? "ORDER BY Q.created ASC" : ($sort == 'votes' || ($sort == '' && $filter == 'noanswers')) ? "ORDER BY Q.vote_count DESC" : ($sort == 'viewed' ? "ORDER BY Q.viewed DESC" : "ORDER BY Q.created DESC" .  ($sort = 'newest') ) );


My Answer:
'ORDER BY date ASC' : 'ORDER BY date DESC');Long IF PHP Code$sort = 'newest';$sql_code_orderby = '';if ($sort == 'oldest') {
$sql_code_orderby = 'ORDER BY date ASC';
} else {
$sql_code_orderby = 'ORDER BY date DESC';
}

Rate this post

3 of 5 based on 4289 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us