Jump to content

[SOLVED] if statement ternary operator


acctman

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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