Jump to content

Complex if statement


ibda12u

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?
Link to comment
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
Link to comment
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]
Link to comment
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]
Link to comment
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.