Forums

PHP

This topic is locked

if statement inside a if statement

Posted 24 Oct 2001 23:09:59
1
has voted
24 Oct 2001 23:09:59 Keith Slater posted:
Hey I was wondering how would I do an if statement inside a if statement. here is the code I got so far

<?
if($Recordset1->Fields("beforeafter" == "1"
{
if($Recordset1->Fields("size" == "v"
{
echo "<a href=\"#\" onMouseOver=\"hideAll();\"><img src=\"../picture/". $Recordset1->Fields("picture" ." \" height=113 border=0 onClick=\"window.open('largek.php?id=". $Recordset1->Fields("picture". "','popup','width=419,height=213,menubar=no,status=no,resizable=no, toolbar=no,location=no,'); return false;\"></a>";
}
{
if($Recordset1->Fields("size" == "h"
echo "<a href=\"#\" onMouseOver=\"hideAll();\"><img src=\"../picture/". $Recordset1->Fields("picture" ." \" height=113 border=0 onClick=\"window.open('largek.php?id=". $Recordset1->Fields("picture". "','popup','width=725,height=263,menubar=no,status=no,resizable=no, toolbar=no,location=no,'); return false;\"></a>";
}

}
else
{
if($Recordset1->Fields("beforeafter" == "0"
echo "<a href=\"#\" onMouseOver=\"hideAll();\"><img src=\"../picture/". $Recordset1->Fields("picture" ." \" height=113 border=0 onClick=\"window.open('largek.php?id=". $Recordset1->Fields("picture". "','popup','width=" .$Recordset1->Fields("size". ",height=263,menubar=no,status=no,resizable=no, toolbar=no,location=no,'); return false;\"></a>";
}
?>

What happens is it just goes straight to the first one no matter what..

Thanks for the help!

Keith Slater

Replies

Replied 24 Oct 2001 23:25:28
24 Oct 2001 23:25:28 Michael O'Neill replied:
if(whatever1)
--{
--if(whetever2)
----{
----do whatever2
----}
--else
----{
----do whatever2 else
----}
--}
else
--{
--do whatever1 else
--}

Indentation makes if's much less confusing!
Mike.



Edited by - carphone on 10/24/2001 23:28:50
Replied 24 Oct 2001 23:49:10
24 Oct 2001 23:49:10 Keith Slater replied:
Their indented on my page and its set up like that also...

Keith Slater
Replied 25 Oct 2001 08:48:38
25 Oct 2001 08:48:38 Bruno Mairlot replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
Hey I was wondering how would I do an if statement inside a if statement. here is the code I got so far
<pre id=code><font face=courier size=2 id=code>
&lt;?
if($Recordset1-&gt;Fields("beforeafter" == "1"
{
if($Recordset1-&gt;Fields("size" == "v"
{
echo "&lt;a .... "&gt;&lt;/a&gt;";
<font color=red>}
{</font id=red>
if($Recordset1-&gt;Fields("size" == "h"
echo "&lt;a ... a&gt;";
}

}
else
{
if($Recordset1-&gt;Fields("beforeafter" == "0"
echo "&lt;a ... &lt;a&gt;";
}
?&gt;
</font id=code></pre id=code>
What happens is it just goes straight to the first one no matter what..

<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Isn't there missing the "else" part where the }{ is in <font color=red>red</font id=red> ?

When talking about the first one, you're talking about the "beforeafter==1" condition, aren't you ?

And I suppose that if you ask the question, you've checked that there IS rows that have 0 value for this column in your recordset ?

Have you checked that the page shows correctly if you delete the inside if statement in first condition ?


--- Better to die trying, than never try at all ---
Replied 25 Oct 2001 14:56:12
25 Oct 2001 14:56:12 Keith Slater replied:
Yea I noted that right after I posted it and I put that in but it still dont work..... And it does work without the inside statement

Keith Slater
Replied 25 Oct 2001 17:32:58
25 Oct 2001 17:32:58 Tim Green replied:
<pre id=code><font face=courier size=2 id=code>
&lt;?php
if ($Recordset1-&gt;Fields("beforeafter" == "1" {
if ($Recordset1-&gt;Fields("size" == "v" {
echo "&lt;a ... &gt;&lt;/a&gt;";
} else if ($Recordset1-&gt;Fields("size" == "h" {
echo "&lt;a ... &gt;&lt;/a&gt;";
}
} else if ($Recordset1-&gt;Fields("beforeafter" == "0" {
echo "&lt;a ... &lt;a&gt;";
}
}
?&gt;

Never underestimate the power of the elseif combination.

Hope this helps.
</font id=code></pre id=code>

Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>
Replied 25 Oct 2001 17:39:40
25 Oct 2001 17:39:40 Bruno Mairlot replied:
Not sure that

<pre id=code><font face=courier size=2 id=code>
if(...){
}
else{
if(...){
}
}
</font id=code></pre id=code>

and
<pre id=code><font face=courier size=2 id=code>
if(...){
}
else if(...){

}
</font id=code></pre id=code>

are semantically different if there's nothing after the second if expression in the first version. At least it shouldn't.

Though, in this case, the second version is preferable.



--- Better to die trying, than never try at all ---
Replied 25 Oct 2001 17:40:29
25 Oct 2001 17:40:29 Tim Green replied:
A more manageable way to do this though is by using the Switch..Case set of conditional commands. This turns your script into :-

<pre id=code><font face=courier size=2 id=code>
&lt;?php
switch ($Recordset1-&gt;Fields("beforeafter") {

case "1":
switch ($Recordset1-&gt;Fields("size") {
case "v":
echo "&lt;a ...&gt;&lt;/a&gt;";
break;
case "h":
echo "&lt;a ...&gt;&lt;/a&gt;";
break;
}
break;

case "0":
echo "&lt;a ...&gt;&lt;/a&gt;";
break;

}
?&gt;
</font id=code></pre id=code>

Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>
Replied 25 Oct 2001 17:44:53
25 Oct 2001 17:44:53 Bruno Mairlot replied:
Tim,

you'll said I'm being stubborn <img src=../images/dmxzone/forum/icon_smile_tongue.gif border=0 align=middle>, but it doesn't change anything !!!

Everything suggested does the same, and we still don't know why this expression is going through the first case in all case...<img src=../images/dmxzone/forum/icon_smile_big.gif border=0 align=middle>

Bruno

--- Better to die trying, than never try at all ---
Replied 25 Oct 2001 17:50:51
25 Oct 2001 17:50:51 Tim Green replied:
Yes you are being stubborn Bruno <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>, as with a Switch..Case it's much easier to debug. If that code is used, then the real truth of the matter is that $Recordset1-&gt;Fields("beforeafter" == "1" and that $Recordset1-&gt;Fields("size" == "v".

I am suggesting that perhaps it's not the logic of this comparison that is at fault, rather that if this comparison is inside a loop perhaps a simple $Recordset1-&gt;MoveNext() is missing, to move the recordset onto the next record. Otherwise this condition is always testing the same data.

Remember the old Acronym. GIGO = Garbage In Garbage Out.

Tim Green

Extension & PHP TalkZone Manager
<font size=1>-------------------------------------------
<i>Please read the Forum FAQ before posting
a question to this TalkZone.</i>
-------------------------------------------
www.UDzone.com : A dynamic Dreamweaver,
Ultradev and Fireworks site for developers
by developers.
-------------------------------------------</font id=size1>

Edited by - rawveg on 10/25/2001 17:52:33
Replied 25 Oct 2001 18:00:08
25 Oct 2001 18:00:08 Bruno Mairlot replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
I am suggesting that perhaps it's not the logic of this comparison that is at fault, rather that if this comparison is inside a loop perhaps a simple $Recordset1-&gt;MoveNext() is missing, to move the recordset onto the next record. Otherwise this condition is always testing the same data.
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Keith said that with removing the second if statement, it does work. So I don't think it is the movenext that is in fault, but rather I did ask him to make sure that the recordset has, without any doubt, different values for the row size. But of that, we are not sure!


Why I'm being stubborn on this point, is because I think the logic IS correct. My apologies for that <img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle>

<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
Remember the old Acronym. GIGO = Garbage In Garbage Out.
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
Mmmh ? Didn't know about that one <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

--- Better to die trying, than never try at all ---
Replied 25 Oct 2001 18:08:25
25 Oct 2001 18:08:25 Keith Slater replied:
heh I guess I should of replied when I figured it out earlier ;P

Thanks anyway!!


Keith Slater
Replied 25 Oct 2001 18:10:23
25 Oct 2001 18:10:23 Bruno Mairlot replied:
But could you please explain what is was about ? I'd like to know, and I'm sure Tim also.

Bruno

--- Better to die trying, than never try at all ---
Replied 25 Oct 2001 18:12:06
25 Oct 2001 18:12:06 Keith Slater replied:
eerhhmmmm.. eh well It was working the whole time it just appeared to me that it wasnt hehehe

Keith Slater

Reply to this topic