Hello!

I was wondering if someone can take a look at the following if and else statement and point out what I have written incorrectly. In its current form, I cannot get it to work. So, essentially, I'm trying to construct an if conditional statement within another if statement.

        $var_test ="25";

       $play_envir ="Open Play"; 

       //In my complete code; $play_envir variable could be any of the following:"Open Play, Robotic Training, Coaching Session "

if($var_test >'0' )
 //if credit exists, payment will be deducted from credit
 {
        if ( $play_envir == "Open Play"  ) {
        echo "Your fee for today is <font color='white'>$$open_play.00</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
        }
        elseif ( $play_envir == "Robotic Training" ) {
        echo "Your fee for today is <font color='white'>$$robotic_play.00 per 30 minute block</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
        }
        elseif ( $play_envir == "Coaching Session" ) {
        echo "<font color='red'>Please see Club Management for today's  coaching fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
        }
        elseif ( $play_envir == "Open Play and Coaching Session" ) {
        echo "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
        }
        elseif ( $play_envir == "Robotic Training and Coaching Session" ) {
        echo "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
        }
        elseif ( $play_envir == "Open Play and Robotic Training" ) {
        echo "For these selections, please pay a <font color='white'>$$play_combo.00 deposit</font>, the balance after playing!<br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
        }
        elseif ( $play_envir == "Open Play and Robotic Training and Coaching Session" ) {
        echo "<font color='red'>Please see Club Management for today's  fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
        }

        }
       // else we need a payment amount indicated
  else
             {

        if ($play_envir == "Open Play" ) {

        echo "Your fee for today is <font color='white'>$$open_play.00</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
        }
        if ( $play_envir == "Robotic Training" ) {
        echo "Your fee for today is <font color='white'>$$robotic_play.00 per 30 minute block</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
        }
        elseif ( $play_envir == "Coaching Session" ) {
        echo "<font color='red'>Please see Club Management for today's  coaching fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
        }
        elseif ( $play_envir == "Open Play and Coaching Session" ) {
        echo "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
        }
        elseif ( $play_envir == "Robotic Training and Coaching Session" ) {
        echo "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
        }
        elseif ( $play_envir == "Open Play and Robotic Training" ) {
        echo "For these selections, please pay a <font color='white'>$$play_combo.00 deposit</font>, the balance after playing!<br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
        }
        elseif ( $play_envir == "Open Play and Robotic Training and Coaching Session" ) {
        echo "<font color='red'>Please see Club Management for today's  fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";
        }

        }

I would appreciate any thoughts on this.

Mossa

Recommended Answers

All 3 Replies

Have a look at switch.

Priteas is of course right a switch statement could do the job , but I am not a fun of switch statements in any language so for me an if – else if – else still is great . BUT : here what you are doing has no logic inside those ifs, so the main point is that if variable a is “x” then variable b is “y” and therefore you could use a more comprehensive syntax.

Example in your code:

<?php
$var_test ="25";
$play_envir ="Open Play";

$options = array();
$options[0]["Open Play"] = "Your fee for today is <font color='white'>$$open_play.00</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";

$options[0]["Robotic Training"] = "Your fee for today is <font color='white'>$$robotic_play.00 per 30 minute block</font> <br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";

$options[0]["Coaching Session"] = "<font color='red'>Please see Club Management for today's coaching fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";

$options[0]["Open Play and Coaching Session"] = "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";

$options[0]["Open Play and Robotic Training"] = "For these selections, please pay a <font color='white'>$$play_combo.00 deposit</font>, the balance after playing!<br /><font color='red' size='8'><img border='0' src='images/form-flashing-arrow.gif' width='65' height='36'>Please Pay First!</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";

$options[0]["Open Play and Robotic Training and Coaching Session"] = "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";

$options[1]["Open Play"] ="Your fee for today is <font color='white'>$$open_play.00</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /> <font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";

$options[1]["Robotic Training"] = "Your fee for today is <font color='white'>$$robotic_play.00 per 30 minute block</font> <br /><img border='0' src='images/form-flashing-arrow.gif' width='45' height='26'><span style='background-color:white'><font color='green' size='5'>Payment Will be Deducted from your Credit on file!</font></span><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";

$options[1]["Coaching Session"] = "<font color='red'>Please see Club Management for today's coaching fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";

$options[1]["Open Play and Coaching Session"] = "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";

$options[1]["Open Play and Robotic Training"] = "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";

$options[1]["Open Play and Robotic Training and Coaching Session"] = "<font color='red'>Please see Club Management for today's fee</font><br /><font color='white' size='3'>Thank you $firstname, and have a great work-out!</font>";


//In my complete code; $play_envir variable could be any of the following:"Open Play, Robotic Training, Coaching Session "
if($var_test >'0' )
{
  //if credit exists, payment will be deducted from credit
  if(isset($options[1][$play_envir]))
  {
    echo $options[1][$play_envir];
  }
  else
  {
    echo "Sorry we don't have that option";
  }
}
// else we need a payment amount indicated
else
{
  if(isset($options[0][$play_envir]))
  {
    echo $options[0][$play_envir];
  }
  else
  {
    echo "Sorry we don't have that option";
  }
}
?>

BUT:
- First of all you have double $ signs in some of your variables … e.g. $$robotic_play
and then ...
- If it is just a map why don’t you use a db to make it more clean and changeable?

even a properties file if not DB with those options could be more helpfull

commented: Thought about your example, but was not willing to type it :) +14

Jkon,

Appreciate your post and your suggested solution. I have also corrected the issue with the double "$". On implementation of your suggestion, everything works great!

Thanks again!

Best
Mossa

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.