Jump to content

[SOLVED] if statement ternary operator


acctman

Recommended Posts

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')");

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'
)");

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?

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 =)

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.