Jump to content

simplified if ... ifelse?


turpentyne

Recommended Posts

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

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);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.