turpentyne Posted March 8, 2012 Share Posted March 8, 2012 Is there a more simplified way of doing a query conditional based on 2 possible options (or more?). If I remember right, there's an operator in javascript that does this simplification. right now I have: if (isset($_POST['print_change']) && ($_POST['print_change'] = "no")){ $query_print = "UPDATE tbl_registration SET reg_hardcopied = '0' WHERE reg_id = '$id_printed'"; mysql_query($query_print); } elseif (isset($_POST['print_change']) && ($_POST['print_change'] = "yes")){ $query_print = "UPDATE tbl_registration SET reg_hardcopied = '1' WHERE reg_id = '$id_printed'"; mysql_query($query_print); } Link to comment https://forums.phpfreaks.com/topic/258498-simplified-if-ifelse/ Share on other sites More sharing options...
xyph Posted March 8, 2012 Share Posted March 8, 2012 Check out the ternary operator http://php.net/manual/en/language.operators.comparison.php echo $condition == true ? 'The condition was true' : 'The condition was false'; Link to comment https://forums.phpfreaks.com/topic/258498-simplified-if-ifelse/#findComment-1325055 Share on other sites More sharing options...
scootstah Posted March 8, 2012 Share Posted March 8, 2012 There's a couple problems with using the ternary operator in this case. The first is that, it is sort of ugly when using an else-if. The second is that you have to set a value for when the condition fails. So you would have to do something like $var = condition1 ? 0 : condition2 ? 1 : someothervalue and then you would have to make sure it is one of the two values...which sort of defeats the purpose. Now, you could instead first check if print_change is set, and then use a ternary...which would be a lot cleaner. Something like: if (isset($_POST['print_change'])) { $reg_hardcopied = $_POST['print_change'] == 'no' ? 0 : 1; $query_print = "UPDATE tbl_registration SET reg_hardcopied = '$reg_hardcopied' WHERE reg_id = '$id_printed'"; mysql_query($query_print); } Link to comment https://forums.phpfreaks.com/topic/258498-simplified-if-ifelse/#findComment-1325060 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.