Jump to content


Photo

if else condition


  • Please log in to reply
10 replies to this topic

#1 firedrop84

firedrop84
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 29 March 2006 - 12:56 PM

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

#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 29 March 2006 - 01:01 PM

[!--quoteo(post=359639:date=Mar 29 2006, 07:56 AM:name=firedrop)--][div class=\'quotetop\']QUOTE(firedrop @ Mar 29 2006, 07:56 AM) View Post[/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]


#3 nikhilthecool

nikhilthecool
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 29 March 2006 - 01:02 PM

as far as i know, u cannot echo or print anything before the header.. jus confirm it.

thanks.

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 29 March 2006 - 01:02 PM

You might try indenting your code so its readbale. You where missing a curly brace.
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');
}


#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 March 2006 - 03:14 PM

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:
<?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');
?>
You can also do away with the if/elseif by using a switch() statement:
<?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');
?>

Ken


#6 firedrop84

firedrop84
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 29 March 2006 - 04:24 PM

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";



#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 March 2006 - 04:49 PM

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:
<?php echo '<pre>' . print_r($Data,true). '</pre>'; ?>
and see what you're really dealing with.

Ken

#8 firedrop84

firedrop84
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 29 March 2006 - 05:00 PM

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 :)

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 March 2006 - 05:03 PM

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

#10 firedrop84

firedrop84
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 29 March 2006 - 05:37 PM

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

#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 March 2006 - 06:12 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users