Forum Moderators: coopster

Message Too Old, No Replies

if inside of an if?

is this possible and how?

         

ajs83

6:22 am on Mar 23, 2005 (gmt 0)

10+ Year Member



Can you do an if inside of another if? Or how do you set it up where something must meet two conditions?

willybfriendly

6:29 am on Mar 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your question is not clear to me. Could be

if(condition_a && condition_b)
action;

or, it could be:

if(condition_a)
{
if(condition_b)
action_b;
}
action_a
}

WBF

charlier

6:29 am on Mar 23, 2005 (gmt 0)

10+ Year Member



Sure

You can have
if($a == "b" && $b == "e"){
print "its a match";
}

or
if($a == "b"){
if($b == "e"){
print "its a match";
}
}

I would use the first unless I wanted to do some processing
if the first test was met as well if both were met.

ajs83

6:35 am on Mar 23, 2005 (gmt 0)

10+ Year Member



What I mean is there are three 3 conditions that need to be met for something to be displayed

1. id must be entered, otherwise they are told to enter an id
2. session id must match the user id associated with the db entry
3. 'status' column in the db must have a certain text.

All need to be matched or they will not be able to view the page but instead shown an error page informing them how to correct it based on the problem.

grandpa

6:57 am on Mar 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I regularly use nested if statements, and while-if statements, and nested while statements.

Even while I admit using them in my PHP code, it is my opinion that it's bad form. My best excuse is that I have not mastered the semantics of PHP. I've seen nested if conditions bring perfectly good programmers to their knees; ie., I once had the pleasure of re-writing a large program that consisted of an if-else condition, then each of those conditions were followed with hundreds of if-if-else-if-else-while-if type tests. It was a nightmare to maintain, and not much better to have to re-write.

<My 2 cents>
Use them sparingly and only as a last recourse. Your code will be much better for it, then one day you'll be happier, make more money, and live your retirement fantasy.

So, here you go...

1. id must be entered, otherwise they are told to enter an id
2. session id must match the user id associated with the db entry
3. 'status' column in the db must have a certain text.

if ($id!= NULL)
{
if ($sessid == $dbsess)
{
if ($status == $db[column])
{

// all three conditions are met
// show them the screen

} //endif $status
else
{
// no match in the db for $status
}
} // endif $sessid
else
{
// did not find the right session id
}
} // endif $id
else
{
echo "Gimme your ID";
}

I think that's right. I added the else conditions.

wrightee

8:03 am on Mar 24, 2005 (gmt 0)

10+ Year Member



Or:

1. id must be entered, otherwise they are told to enter an id
2. session id must match the user id associated with the db entry
3. 'status' column in the db must have a certain text.

if(!$id){$err[]="Er, id?";}
if(!$session_id==$user_id){$err[]="Tut, tut, session?";}
if(!$status!="fish and chips"){$err[]="I'm hungry";}

if(sizeof($err)){
// do error stuff.. print_r($err)
}
else{
// away you go
}

lZakl

7:39 pm on Mar 25, 2005 (gmt 0)

10+ Year Member



Couldn't && be used here to simplify?

if (Condition1 && Condition2 && Condition3)
{
execute your code
}

else //This would be the error handling
{
if (Condition1 == false)
{
echo"There was an error with condition 1";
}

if (Condition2 == false)
{
echo"There was an error with condition 2";
}

if (Condition3 == false)
{
echo"There was an error with condition 3";
}
}

ajs83

8:02 am on Mar 26, 2005 (gmt 0)

10+ Year Member



Another variation of this question. I currently have an "if" conditional, but I want to move that to another section which is only shown if certain conditions are met. How would I do that without confusing the two?

Here's the code currently

echo "$rbname<br>$rbimage<br>$rbdescription<br>$rbpoints<br>";
if($rab > $rb) {
echo "You have enough";
} else {
echo "You dont have enough";
}

==========================================
What I would like to do if put it inside of this

if($earmark > 0) {
echo "You have enough points to get this item";
} else {
echo "You still need to earn $rbtotal points to get this item";
}
==========================================
under the "if($earmark > 0) {" section

ajs83

9:35 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



...

jatar_k

9:57 pm on Mar 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the way I often do it is to write it out first, then code it later. I know, pencil and paper, they're hard to find these days but word is they still work fairly well. ;)

if $rab is greater than $rb then they have enough points

if $rab is not greater than $rb but $earmark is greater than 0 they also have enough points

if none of the above case are met then they do not have enough points

is that the logic you are looking for?

ajs83

10:03 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



I do that for more pressing items and it works great.

What I want to do is have it like this

if($earmark = 0) {
echo "<br>";
} else {

then it needs to show a table which is coded as follows
echo "$rbname<br>$rbimage<br>$rbdescription<br>$rbpoints<br>";
if($rab > $rb) {
echo "You have enough";
} else {
echo "You dont have enough";
}

I just am befuddled on how I could include the second if (if($rab > $rb) { ) without confusing it with the first.

ergophobe

10:09 pm on Mar 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




the way I often do it is to write it out first, then code it later. I know, pencil and paper

Luddite!

Grandpa, there's nothing wrong with nesting if statements and it's usually necessary to do anything at all. Sometimes it makes things clearer than having a lengthy list of conditions in if-elseif-else blocks.

One little trick to keep in mind though, is that if your ifs are in function...

instead of

if (conditiona){
do a
}
elseif (conditionb) {
do b
}
else (conditionc) {
do c
}

it is sometimes clearer to do

if (conditiona){
return a
}

if (conditionb) {
return b
}

return c