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
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
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
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
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
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
Share on other sites

this is what I thought first. I used already the print_r and I suprised that the first element in the array was 1. I dont know why. Then, I displayed the $HealthCover variable and the value of it was as well No. so that means it's correct. ??!!? strange error I think :)
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.