Jump to content

Archived

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

firedrop84

if else condition

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');
}

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
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

Share this post


Link to post
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";

Share this post


Link to post
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

Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites
Can you post the code you use to read the data and populate the array? Also, what is the format of the data file?

Ken

Share this post


Link to post
Share on other sites
I dont know why I cann't post the code. it's display error 403 and it's saying you cannot view this page when I click on replay the post

Share this post


Link to post
Share on other sites
There are some problems posting code on this forum when the code contains filesystem function. In order to get around this, try putting an illegal character in the function name like "f.open()".

Ken

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.