ibda12u Posted June 24, 2006 Share Posted June 24, 2006 Hello I'm trying to write a complex if statement.Any idea why this doesn't work? or what is the best way to write this?[code] <?php If ($foldertype == "A" || "B" && $quantity == "500") {$iprice = $TempAB500}; If ($foldertype == "A" || "B" && $quantity == "1000") {$iprice = $TempAB1000}; If ($foldertype == "A" || "B" && $quantity == "2000") {$iprice = $TempAB2000}; If ($foldertype == "A" || "B" && $quantity == "5000") {$iprice = $TempAB5000}; ?>[/code]$foldertype is a dropdown form, where someone can select either Folder A, B, C, or D. I want the if to work if they selected A or B. $quantity is another dropdown. The TempAB500 variables are just prices.Any ideas? Link to comment https://forums.phpfreaks.com/topic/12818-complex-if-statement/ Share on other sites More sharing options...
AndyB Posted June 24, 2006 Share Posted June 24, 2006 [code]if (($foldertype=="A") || ($foldertype=="B")) { if ($quantity == "500") { $iprice = $TempAB500; } // more comparisons of quantity} else { // guess you chose C or D}[/code] Link to comment https://forums.phpfreaks.com/topic/12818-complex-if-statement/#findComment-49145 Share on other sites More sharing options...
kenrbnsn Posted June 24, 2006 Share Posted June 24, 2006 The if statement should look like:[code]<?php if (($foldertype == "A" || $foldertype == "B") && $quantity == "500") ?>[/code]But since the first part of the "if" is always the same, do that once and then use a switch statement:[code]<?phpif ($foldertype == 'A' || $foldertype == 'B') switch ($quantity) case '500': $iprice = $TempAB500; break; case '1000': $iprice = $TempAB1000; break; case '2000': $iprice = $TempAB2000; break; case '5000': $iprice = $TempAB5000; break;}?>[/code]Another option would depend on how the $Temp... variables are being initialized. If you can put them in an array, then the solution boils down to a simple "if". Something like:[code]<?php$TempAB = array('500'=>'value of TempAB500', '1000'=> 'value of TempAB1000', '2000'=> 'value of TempAB2000', '5000'=> 'value of TempAB5000')if ($foldertype == 'A' || $foldertype == 'B') $iprice = $TempAB[$quantity];?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/12818-complex-if-statement/#findComment-49147 Share on other sites More sharing options...
Koobi Posted June 24, 2006 Share Posted June 24, 2006 to add to kenrbnsn's post, you can also do this:[code]<?phpif ($foldertype == 'A' || $foldertype == 'B'){ switch ($quantity){ case '500': case '1000': case '2000': case '5000': $iprice = $TempAB . $quantity; break; default: $iprice = 0; break;}}?>[/code]but i've taken the liberty of assuming that the value of $quantity will be a part of $iprice so this is conditional.:edit:i added a 'default' case to be on the safe side.:edit 2:you can also do this if you prefer:[code]<?php$allowed = array('500', '1000', '2000', '5000');if (($foldertype == 'A' || $foldertype == 'B') && in_array($quantity, $allowed)){ $iprice = $TempAB . $quantity;}?>[/code] Link to comment https://forums.phpfreaks.com/topic/12818-complex-if-statement/#findComment-49166 Share on other sites More sharing options...
ibda12u Posted June 24, 2006 Author Share Posted June 24, 2006 Wow, everyone awesome awesome. I didn't even think about the switch option!Thanks a ton![!--quoteo(post=387553:date=Jun 24 2006, 03:33 PM:name=Bane)--][div class=\'quotetop\']QUOTE(Bane @ Jun 24 2006, 03:33 PM) [snapback]387553[/snapback][/div][div class=\'quotemain\'][!--quotec--]to add to kenrbnsn's post, you can also do this:[code]<?phpif ($foldertype == 'A' || $foldertype == 'B'){ switch ($quantity){ case '500': case '1000': case '2000': case '5000': $iprice = $TempAB . $quantity; break; default: $iprice = 0; break;}}?>[/code]but i've taken the liberty of assuming that the value of $quantity will be a part of $iprice so this is conditional.:edit:i added a 'default' case to be on the safe side.:edit 2:you can also do this if you prefer:[code]<?php$allowed = array('500', '1000', '2000', '5000');if (($foldertype == 'A' || $foldertype == 'B') && in_array($quantity, $allowed)){ $iprice = $TempAB . $quantity;}?>[/code][/quote] Link to comment https://forums.phpfreaks.com/topic/12818-complex-if-statement/#findComment-49202 Share on other sites More sharing options...
ibda12u Posted June 25, 2006 Author Share Posted June 25, 2006 Okay, now this part here is even more difficult than the first I believe [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /] I have an include page that lists these variables[code]$TABSSC1Q500 = '196.00';$TABSSC1Q1000 = '264.00';$TABSSC1Q2000 = '401.00';$TABSSC1Q5000 = '777.00';[/code]on my main page I have this code[code]<?phpif ($frontcls =="1" && $foldertype == 'Folder A' || $foldertype == 'Folder B') { switch($quantity) { case '500': case '1000': case '2000': case '5000': $t = $TABSSC1Q . $quantity; break;}}$scprice = $t;?>[/code]What I'm trying to do is get $t to become a variable from my include page. So I can return the value of that variable. Is it possible to create a variable from 2 others? When I run this code as it is, it only displays the value of quantity. I tried $t = "$".TABSSC1Q . $quantity;but it simple returned the string $TABSSC1Q500 (if the quantity was 500), but it does return the actual value of the variable $TABSSC1Q500Any idea's? Link to comment https://forums.phpfreaks.com/topic/12818-complex-if-statement/#findComment-49276 Share on other sites More sharing options...
ibda12u Posted June 25, 2006 Author Share Posted June 25, 2006 I figured it out by doing this[code] $t = "TABSSC1Q" . $quantity; break;}}$scprice = ${$t};[/code] Link to comment https://forums.phpfreaks.com/topic/12818-complex-if-statement/#findComment-49363 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.