Page 1 of 2
Weird things with if statement or "how to lose your min
Posted: Wed Jun 21, 2006 12:51 pm
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.
Posted: Wed Jun 21, 2006 1:16 pm
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"
Posted: Wed Jun 21, 2006 1:22 pm
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?
Posted: Wed Jun 21, 2006 1:35 pm
by Milan
ohhhh i see it now,
human stupidity knows no limits.
thanks !
Posted: Wed Jun 21, 2006 1:59 pm
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.
Posted: Wed Jun 21, 2006 2:03 pm
by Milan
AWESOME !
Posted: Wed Jun 21, 2006 2:08 pm
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);
Posted: Wed Jun 21, 2006 2:21 pm
by Milan
now when i compare that with my code, i am just depressed
lol thanks guys!
Posted: Wed Jun 21, 2006 2:45 pm
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

Posted: Wed Jun 21, 2006 2:49 pm
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);

Posted: Wed Jun 21, 2006 2:50 pm
by John Cartwright
Ugly use of @ though...

Posted: Wed Jun 21, 2006 2:52 pm
by Weirdan
or

Posted: Wed Jun 21, 2006 2:53 pm
by Weirdan
Jcart wrote:Ugly use of @ though...

Well, we talk about hacks here

Posted: Wed Jun 21, 2006 2:55 pm
by John Cartwright
Weirdan wrote:Jcart wrote:Ugly use of @ though...

Well, we talk about hacks here

Fair enough..

Posted: Wed Jun 21, 2006 4:26 pm
by bdlang
Oh c'mon you guys, make me look bad.
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.