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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.