Jump to content

Archived

This topic is now archived and is closed to further replies.

ibda12u

Complex if statement

Recommended Posts

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?

Share this post


Link to post
Share on other sites
[code]
if (($foldertype=="A") || ($foldertype=="B")) {
   if ($quantity == "500") { $iprice = $TempAB500; }
   // more comparisons of quantity

} else {
    // guess you chose C or D
}[/code]

Share this post


Link to post
Share on other sites
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]<?php
if ($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

Share this post


Link to post
Share on other sites
to add to kenrbnsn's post, you can also do this:

[code]
<?php
if ($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]

Share this post


Link to post
Share on other sites
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]
<?php
if ($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]

Share this post


Link to post
Share on other sites
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]
<?php
if ($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 $TABSSC1Q500

Any idea's?

Share this post


Link to post
Share on other sites
I figured it out by doing this
[code]
   $t = "TABSSC1Q" . $quantity;
          break;
}
}
$scprice = ${$t};[/code]

Share this post


Link to post
Share on other sites

×

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.