Jump to content

if else condition


firedrop84

Recommended Posts

I am just wonding where is the error that i got as the if and else condition it's not working. I dont know why?!??!

here is the code


if ($HealthCover == "No")
{
print "It has been passed to NO";
if($achievement == "Toning" && $Gender == "Male")
{
print ("Men Transformation");
}
elseif ($achievement == "Toning" && $Gender == "Female")
{
print "Womens Transformation";
}
elseif ($achievement == "MuscleGain" && $Gender == "Male")
{
print "Mens muscle building";
}
elseif ($achievement == "WeightGain" && $Gender == "Female")
{
print "Womens Body Shaping";
}
else
{
header('Location: EnquiryForm.php');
}
Link to comment
https://forums.phpfreaks.com/topic/6094-if-else-condition/
Share on other sites

[!--quoteo(post=359639:date=Mar 29 2006, 07:56 AM:name=firedrop)--][div class=\'quotetop\']QUOTE(firedrop @ Mar 29 2006, 07:56 AM) [snapback]359639[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I am just wonding where is the error that i got as the if and else condition it's not working. I dont know why?!??!

here is the code


if ($HealthCover == "No")
{<-----------------------------------------where is the close for this open brace?????
print "It has been passed to NO";
if($achievement == "Toning" && $Gender == "Male")
{
print ("Men Transformation");
}
elseif ($achievement == "Toning" && $Gender == "Female")
{
print "Womens Transformation";
}
elseif ($achievement == "MuscleGain" && $Gender == "Male")
{
print "Mens muscle building";
}
elseif ($achievement == "WeightGain" && $Gender == "Female")
{
print "Womens Body Shaping";
}
else
{
header('Location: EnquiryForm.php');
}
[/quote]
Link to comment
https://forums.phpfreaks.com/topic/6094-if-else-condition/#findComment-21932
Share on other sites

You might try indenting your code so its readbale. You where missing a curly brace.
[code]if ($HealthCover == "No") {
    print "It has been passed to NO";
    if($achievement == "Toning" && $Gender == "Male") {
        print ("Men Transformation");
    } elseif ($achievement == "Toning" && $Gender == "Female") {
        print "Womens Transformation";
    } elseif ($achievement == "MuscleGain" && $Gender == "Male") {
        print "Mens muscle building";
    } elseif ($achievement == "WeightGain" && $Gender == "Female") {
        print "Womens Body Shaping";
    }
} else {
    header('Location: EnquiryForm.php');
}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/6094-if-else-condition/#findComment-21934
Share on other sites

Since curly braces are not required when the statement block that is contained by them is only one statement, most of the braces can be removed:
[code]<?php
if ($HealthCover == "No") {
    print "It has been passed to NO";
    if($achievement == "Toning" && $Gender == "Male") print "Men Transformation";
    elseif ($achievement == "Toning" && $Gender == "Female") print "Womens Transformation";
    elseif ($achievement == "MuscleGain" && $Gender == "Male") print "Mens muscle building";
    elseif ($achievement == "WeightGain" && $Gender == "Female") print "Womens Body Shaping";
} else
    header('Location: EnquiryForm.php');
?>[/code]
You can also do away with the if/elseif by using a switch() statement:
[code]<?php
if ($HealthCover == "No") {
    print "It has been passed to NO";
    switch ($achievment) {
        case "Toning":
            if($Gender == "Male") print "Men Transformation";
            if($Gender == "Female") print "Womens Transformation";
        break;
        case "MuscleGain":
            if($Gender == "Male") print "Mens muscle building";
            break;
        case "WeightGain":
            if($Gender == "Female") print "Womens Body Shaping";
            break;
    }
} else
    header('Location: EnquiryForm.php');
?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/6094-if-else-condition/#findComment-21963
Share on other sites

thanx for all the replies.

my code still doesn't work as all the variables that I have it comes from a text file. then it puts them into an array after that into different variable names. Finally use the the print statment.

I have used the code that ken adviced me and I have used before the if a print just to test what is inside the HealthCover variable. It has No varaible. but it doesnt go throught the if statment it goes staright to else. anybody got any idea why its happening like that.

$Gender = $Data[1];
$achievement = $Data[2];
$Programs = $Data[3];
$WhatAchieve = $Data[4];
$WhenExercise = $Data[5];
$ComeToGym = $Data[6];
$IndoorsOutdoors = $Data[7];
$HowMuch = $Data[8];
$HealthCover = $Data[9];


print "$HealthCover";

if ($Data[9] == "No") {
print "It has been passed to NO";
switch ($achievment) {
case "Toning":
if($Gender == "Male") print "Men Transformation";
if($Gender == "Female") print "Womens Transformation";
break;
case "MuscleGain":
if($Gender == "Male") print "Mens muscle building";
break;
case "WeightGain":
if($Gender == "Female") print "Womens Body Shaping";
break;
}
} else
print "Didn't Read the if";

Link to comment
https://forums.phpfreaks.com/topic/6094-if-else-condition/#findComment-21981
Share on other sites

Are you sure the last value is in $Data[9]? Array indices start at 0 in PHP. After putting you data into the array, dump it to the screen using the following code:
[code]<?php echo '<pre>' . print_r($Data,true). '</pre>'; ?>[/code]
and see what you're really dealing with.

Ken
Link to comment
https://forums.phpfreaks.com/topic/6094-if-else-condition/#findComment-21994
Share on other sites

Archived

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

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