Weird things with if statement or "how to lose your min

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

Weird things with if statement or "how to lose your min

Post by Milan »

i have a simple, dumb IF statements:

Code: Select all

$type = $row_Recordset3['ptype'];
mysql_select_db($database_pickageek, $pickageek);
if ($type=="unkn"){
$query3 = "UPDATE `awapr` set `pricemin` = '1' where `session` = '$sess'";
}
if ($type=="unkn"){
$query3 = "UPDATE `awapr` set `pricemin` = '1' where `session` = '$sess'";
}
if ($type=="smlb"){
$query3 = "UPDATE `awapr` set `pricemin` = '100' where `session` = '$sess'";
}
if ($type=="medb"){
$query3 = "UPDATE `awapr` set `pricemin` = '500' where `session` = '$sess'";
}
if ($type=="bigb"){
$query3 = "UPDATE `awapr` set `pricemin` = '1000' where `session` = '$sess'";
mysql_query($query3) or die(mysql_error());
}
the problem is , THIS DOES NOT WORK ! It does not enter anyting in the table, am i doing something wrong?

thanks.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$type = $row_Recordset3['ptype'];
mysql_select_db($database_pickageek, $pickageek);

if ($type=="unkn"){
    $query3 = "UPDATE `awapr` set `pricemin` = '1' where `session` = '$sess'";
}

if ($type=="unkn"){
    $query3 = "UPDATE `awapr` set `pricemin` = '1' where `session` = '$sess'";
}

if ($type=="smlb"){
    $query3 = "UPDATE `awapr` set `pricemin` = '100' where `session` = '$sess'";
}

if ($type=="medb"){
    $query3 = "UPDATE `awapr` set `pricemin` = '500' where `session` = '$sess'";
}

if ($type=="bigb"){
    $query3 = "UPDATE `awapr` set `pricemin` = '1000' where `session` = '$sess'";
    mysql_query($query3) or die(mysql_error());
}

with proper indentation your problem is obvious, isn't it? :)
mysql_query() is executed only if $type=="bugb"
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

Post by Milan »

I think i do know what you are trying to say but i still dont get it why it is tied only to the last IF statement, shouldn't it check the first one and then set the query if true and if false move to the next one?
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

Post by Milan »

ohhhh i see it now,

human stupidity knows no limits. :oops:

thanks !
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Please note you have a duplicate conditional there, using 'unkn'.

How about something like this to simplify using a switch() statement, since you are only changing a single element in the SQL statement:
(untested, ymmv)

Code: Select all

$type = $row_Recordset3['ptype'];
mysql_select_db($database_pickageek, $pickageek);

$query3= 'UPDATE `awpr` SET `pricemin` = ';
switch($type) {
    case 'unkn':
        $query3.= 1;
        break;
    case 'smlb':
        $query3.= 100;
        break;
    case 'medb':
        $query3.= 500;
        break;
    case 'bigb':
        $query3.= 1000;
        break;
    default:
        $query3.= 1;
}
$query3.= " WHERE `session` = '{$sess}'";

mysql_query($query3) or die(mysql_error());
You don't have to type the query out over and over, and if there is a new type, just add it to the switch() flow control.

You might also have a simple array and use foreach() to find the right value, e.g.
(untested, ymmv)

Code: Select all

$type_array= array('unkn' => 1, 'smlb' =>100, 'medb' =>500, 'bigb' => 1000);

foreach ($type_array AS $key => $value) {
    if ( $row_Recordset3['ptype'] == $key ) {
        $pricemin= $type_array[$key];
        break;
    }else{
        $pricemin= 1;
    }
}
$query3= sprintf("UPDATE `awpr` SET `pricemin = '%d' WHERE `session`= '%s'", $pricemin, $sess);
Just a couple of different methods, I always try to get away from using big conditionals and writing the same code construct over wherever possible.
Last edited by bdlang on Wed Jun 21, 2006 4:34 pm, edited 1 time in total.
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

Post by Milan »

AWESOME !
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

This could be even simplier:

Code: Select all

$type_array= array('unkn' => 1, 'smlb' =>100, 'medb' =>500, 'bigb' => 1000);

$pricemin = @$type_array[$row_Recordset3['ptype']];
if(!$pricemin)
   $pricemin = 1;

$query3= sprintf("UPDATE `awpr` SET `pricemin = '%d' WHERE `session`= '%s'", $pricemin, $sess);
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

Post by Milan »

now when i compare that with my code, i am just depressed 8O

lol thanks guys!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$type_array= array('unkn' => 1, 'smlb' =>100, 'medb' =>500, 'bigb' => 1000);

$pricemin = (array_key_exists($row_Recordset3['ptype'], $type_array) ? $type_array[$row_Recordset3['ptype']] : 1);

$query3= sprintf("UPDATE `awpr` SET `pricemin = '%d' WHERE `session`= '%s'", $pricemin, $sess);
Even simpler ;)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$type_array= array('unkn' => 1, 'smlb' =>100, 'medb' =>500, 'bigb' => 1000);

$pricemin = @$type_array[$row_Recordset3['ptype']];
$pricemin = $pricemin ? $pricemin : 1;

$query3= sprintf("UPDATE `awpr` SET `pricemin = '%d' WHERE `session`= '%s'", $pricemin, $sess);
:lol:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Ugly use of @ though... :wink:
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

or

Code: Select all

$pricemin += (!(bool)$pricemin);
:lol:
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Jcart wrote:Ugly use of @ though... :wink:
Well, we talk about hacks here :wink:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Weirdan wrote:
Jcart wrote:Ugly use of @ though... :wink:
Well, we talk about hacks here :wink:
Fair enough.. 8)
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Oh c'mon you guys, make me look bad. :D

Great examples, though. I initially thought of using array_key_exists() or in_array() but opted for a quick iteration with foreach(), I was quickly typing it up on lunch.
Last edited by bdlang on Wed Jun 21, 2006 4:30 pm, edited 1 time in total.
Post Reply