acctman Posted July 15, 2009 Share Posted July 15, 2009 is it possible to add 3 different conditions to an IF ternary operator? i'm trying to combine all 3 into one statement ($_SESSION['phfolder'] == '1' ? '3' : '') ($_SESSION['phfolder'] == '2' ? '2' : '') ($_SESSION['phfolder'] == '3' ? '1' : '') Link to comment https://forums.phpfreaks.com/topic/166131-solved-if-statement-ternary-operator/ Share on other sites More sharing options...
genericnumber1 Posted July 15, 2009 Share Posted July 15, 2009 You can probably nest the ternary operators to get this effect, but unless you're trying to give someone a headache, just use normal if/then/elseif/else statements. Link to comment https://forums.phpfreaks.com/topic/166131-solved-if-statement-ternary-operator/#findComment-876139 Share on other sites More sharing options...
sKunKbad Posted July 15, 2009 Share Posted July 15, 2009 This would be a time to use a switch statement. Link to comment https://forums.phpfreaks.com/topic/166131-solved-if-statement-ternary-operator/#findComment-876146 Share on other sites More sharing options...
acctman Posted July 16, 2009 Author Share Posted July 16, 2009 You can probably nest the ternary operators to get this effect, but unless you're trying to give someone a headache, just use normal if/then/elseif/else statements. i ended up doing this @mysql_query("INSERT INTO rate_pictures (i_user, i_status, i_time, i_ip, i_admin, i_name, i_type, i_private, i_pbooth) VALUES ('".$uid."', '0', '".time()."', '". $en['ip'] . "','0','','".$en['m_type']."','".($_SESSION['phfolder'] == '1' ? '3' : ''). ($_SESSION['phfolder'] == '2' ? '2' : '').($_SESSION['phfolder'] == '3' ? '1' : '')."','1')"); Link to comment https://forums.phpfreaks.com/topic/166131-solved-if-statement-ternary-operator/#findComment-876192 Share on other sites More sharing options...
genericnumber1 Posted July 16, 2009 Share Posted July 16, 2009 I weep for you when you come back to this code in 6 months. For now though, I guess it's impressive you got it on one line Link to comment https://forums.phpfreaks.com/topic/166131-solved-if-statement-ternary-operator/#findComment-876229 Share on other sites More sharing options...
Daniel0 Posted July 16, 2009 Share Posted July 16, 2009 Code is first and foremost for humans to read... Link to comment https://forums.phpfreaks.com/topic/166131-solved-if-statement-ternary-operator/#findComment-876250 Share on other sites More sharing options...
acctman Posted July 16, 2009 Author Share Posted July 16, 2009 how's this... i made it a little bit more readable @mysql_query("INSERT INTO rate_pictures (i_user, i_status, i_time, i_ip, i_admin, i_name, i_type, i_private, i_pbooth) VALUES ( '".$uid."', '".($phfolder != '1' ? '1' : '0')."', '".time()."', '".$en['ip']."', '0', 'Photo Booth : '.'".$datepbooth."', '".$en['m_type']."', '".($phfolder == '1' ? '3' : '').($phfolder == '2' ? '2' : '').($phfolder == '3' ? '1' : '')."', '1' )"); Link to comment https://forums.phpfreaks.com/topic/166131-solved-if-statement-ternary-operator/#findComment-876253 Share on other sites More sharing options...
Ken2k7 Posted July 16, 2009 Share Posted July 16, 2009 First of all, are i_user, i_admin, i_status and i_pbooth of type int? If so, they don't need quotes in the SQL string. How about this - <?php $i_status = 1; $i_private = ''; switch ($phfolder) { case '1': $i_status = 0; $i_private = '3'; break; case '2': $i_private = '2'; break; case '3': $i_private = '1'; break; } $sql = sprintf('INSERT INTO rate_pictures (i_user, i_status, i_time, i_ip, i_admin, i_name, i_type, i_private, i_pbooth) VALUES (%d, %d, "%s", "%s", %d, "%s", "%s", "%s", %d)', intval($uid), intval($i_status), mysql_real_escape_string(time()), mysql_real_escape_string($en['ip']), 0, mysql_real_escape_string('Photo Booth: ' . $datepbooth), mysql_real_escape_string($i_private), 1 ); $query = mysql_query($sql) or trigger_error('some error', E_USER_ERROR); Better? Link to comment https://forums.phpfreaks.com/topic/166131-solved-if-statement-ternary-operator/#findComment-876269 Share on other sites More sharing options...
sasa Posted July 16, 2009 Share Posted July 16, 2009 change '".($phfolder == '1' ? '3' : '').($phfolder == '2' ? '2' : '').($phfolder == '3' ? '1' : '')."', to '".(4 - $phfolder)."', Link to comment https://forums.phpfreaks.com/topic/166131-solved-if-statement-ternary-operator/#findComment-876281 Share on other sites More sharing options...
acctman Posted July 16, 2009 Author Share Posted July 16, 2009 First of all, are i_user, i_admin, i_status and i_pbooth of type int? If so, they don't need quotes in the SQL string. How about this - <?php $i_status = 1; $i_private = ''; switch ($phfolder) { case '1': $i_status = 0; $i_private = '3'; break; case '2': $i_private = '2'; break; case '3': $i_private = '1'; break; } $sql = sprintf('INSERT INTO rate_pictures (i_user, i_status, i_time, i_ip, i_admin, i_name, i_type, i_private, i_pbooth) VALUES (%d, %d, "%s", "%s", %d, "%s", "%s", "%s", %d)', intval($uid), intval($i_status), mysql_real_escape_string(time()), mysql_real_escape_string($en['ip']), 0, mysql_real_escape_string('Photo Booth: ' . $datepbooth), mysql_real_escape_string($i_private), 1 ); $query = mysql_query($sql) or trigger_error('some error', E_USER_ERROR); Better? thanks =) Link to comment https://forums.phpfreaks.com/topic/166131-solved-if-statement-ternary-operator/#findComment-876368 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.