Multiple "if()" statements
10 answers - 1374 bytes -

I have a table with lots of fun information in it. For one of the pages
that I am working on, I want to display a list of names based on what is in
the DB.
My $SQL statement works great and has pulled in values for "first_name,"
"hs_last_name," and "last_name" in that order. Right now, I am just
printing to the screen the results of "first_name (hs_last_name) last_name"
but there are two problems with this
The first problem is men's names and unmarried women's names they will
have the same "hs_last_name" and "last_name" so I don't want the duplicate
displaying on the page.
The second problem is the entry of the word "none" by some of the visitors
in place of a "hs_last_name" obviously I don't want to display this
either.
The following is the string of "IF()" statements that I am using, but I
have an issue with my syntax somewhere because it isn't working the way I
want. Any help would be great!
if($row[1]="none") {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
if($row[1]=$row[2]) {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
print("<tr>");
print("<td>$row[0] ($row[1]) $row[2]</td>");
print("</tr>");
No.1 | | 1610 bytes |
| 
Grae Wolfe - PHP wrote:
want. Any help would be great!
if($row[1]="none") {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
if($row[1]=$row[2]) {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
print("<tr>");
print("<td>$row[0] ($row[1]) $row[2]</td>");
print("</tr>");
Indenting is your friend, indented version of what you had.
if($row[1]="none") {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
if($row[1]=$row[2]) {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
print("<tr>");
print("<td>$row[0] ($row[1]) $row[2]</td>");
print("</tr>");
The bigest problem with the above is that both the else becomes unclear
when they finish due to the lack of {}.
The ifs should also be using an == instead of an =, you want to compare
not assign.
I'm also going to throw in an elseif for fun, to get this (hopefully)
improved version:
if($row[1] == "none") {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} elseif($row[1] == $row[2]) {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else {
print("<tr>");
print("<td>$row[0] ($row[1]) $row[2]</td>");
print("</tr>");
}
No.2 | | 2335 bytes |
| 
Wed, 2006-06-28 at 20:02, David Tulloh wrote:
Grae Wolfe - PHP wrote:
want. Any help would be great!
if($row[1]="none") {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
if($row[1]=$row[2]) {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
print("<tr>");
print("<td>$row[0] ($row[1]) $row[2]</td>");
print("</tr>");
Indenting is your friend, indented version of what you had.
if($row[1]="none") {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
if($row[1]=$row[2]) {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
print("<tr>");
print("<td>$row[0] ($row[1]) $row[2]</td>");
print("</tr>");
The bigest problem with the above is that both the else becomes unclear
when they finish due to the lack of {}.
The ifs should also be using an == instead of an =, you want to compare
not assign.
I'm also going to throw in an elseif for fun, to get this (hopefully)
improved version:
if($row[1] == "none") {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} elseif($row[1] == $row[2]) {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else {
print("<tr>");
print("<td>$row[0] ($row[1]) $row[2]</td>");
print("</tr>");
}
And for really clear code
if( $row[1] == "none ")
{
print( "<tr>" );
print( "<td>$row[0] $row[2]</td>" );
print( "</tr>" );
}
else
if( $row[1] == $row[2] )
{
print( "<tr>" );
print( "<td>$row[0] $row[2]</td>" );
print( "</tr>");
}
else
{
print( "<tr>" );
print( "<td>$row[0] ($row[1]) $row[2]</td>" );
print( "</tr>" );
}
Now you know when you've forgotten an opening brace and it lines up
beutifully vertically. Now I need to run and hide before a braces holy
war erupts *Wheeeeeeeeeeeeeeeeeeeeeeeee*.
Cheers,
Rob.
No.3 | | 1326 bytes |
| 
At 04:38 PM 6/28/2006, Grae Wolfe - PHP wrote:
The first problem is men's names and unmarried women's names they will
>have the same "hs_last_name" and "last_name" so I don't want the duplicate
>displaying on the page.
The second problem is the entry of the word "none" by some of the visitors
>in place of a "hs_last_name" obviously I don't want to display this
>either.
>
>
if($row[1]="none") {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
if($row[1]=$row[2]) {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
print("<tr>");
print("<td>$row[0] ($row[1]) $row[2]</td>");
print("</tr>");
Grae,
For ease of maintenance, and to reduce repetition in the code, I
would separate the logic of your string concatenation from the HTML
output, something like this:
$sName = $row[0];
if ($row[1] != "none" && $row[1] != $row[2]) $sName .= $row[1];
$sName .= $row[2];
$sHTML = <<< hdHTML
<tr>
<td>$sName</td>
</tr>
hdHTML;
print($sHTML);
Regards,
Paul
No.4 | | 1803 bytes |
| 
Thank you all S incredibly much!!! Now I just have to figure out how to
get it to put the results into two columns instead of one, and this phase of
this nightmarish project is over!!
I am eternally grateful, and I am sure I will be back with more questions!!
""Grae Wolfe - PHP"" <php (AT) graewolfe (DOT) comwrote in message
news:66.EE.15023.4E213A44 (AT) pb1 (DOT) pair.com
>I have a table with lots of fun information in it. For one of the pages
>that I am working on, I want to display a list of names based on what is in
>the DB.
My $SQL statement works great and has pulled in values for "first_name,"
"hs_last_name," and "last_name" in that order. Right now, I am just
printing to the screen the results of "first_name (hs_last_name)
last_name" but there are two problems with this
The first problem is men's names and unmarried women's names they
will have the same "hs_last_name" and "last_name" so I don't want the
duplicate displaying on the page.
The second problem is the entry of the word "none" by some of the
visitors in place of a "hs_last_name" obviously I don't want to
display this either.
The following is the string of "IF()" statements that I am using, but I
have an issue with my syntax somewhere because it isn't working the way I
want. Any help would be great!
--
if($row[1]="none") {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
if($row[1]=$row[2]) {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
print("<tr>");
print("<td>$row[0] ($row[1]) $row[2]</td>");
print("</tr>");
No.5 | | 1247 bytes |
| 
At 8:15 PM -0400 6/28/06, Robert Cummings wrote:
Wed, 2006-06-28 at 20:02, David Tulloh wrote:
>Grae Wolfe - PHP wrote:
>
want. Any help would be great!
-snip- if/elseif -snip-
<holy war id="opinion">
Whenever you need a elseif, then it's time to consider a switch -- like thus:
print( "<tr>" );
switch $row[1]
{
case: "none";
print( "<td>$row[0] $row[2]</td>" );
break;
case: $row[2];
print( "<td>$row[0] $row[2]</td>" );
break;
default:
print( "<td>$row[0] ($row[1]) $row[2]</td>" );
break;
}
print( "</tr>" );
<humor id="mine">
/* Please note the humor tags, smiley and apology beforehand.
No offense meant to anyone -- insert apology where needed -- and appropriate.
Your mileage may vary, Take only as prescribed by doctor's advice. No hablo */
To me, the switch statement is so much simpler -- what's with you guys and these long-ass and confusing if/elseif structures? :-)
</humor id="mine">
</holy war id="opinion">
tedd <limping off to his bear cave awaiting fallout/>.
No.6 | | 595 bytes |
| 
Thursday 29 June 2006 06:51, tedd wrote:
At 8:15 PM -0400 6/28/06, Robert Cummings wrote:
Wed, 2006-06-28 at 20:02, David Tulloh wrote:
>Grae Wolfe - PHP wrote:
>
>>
want. Any help would be great!
-snip- if/elseif -snip-
--
<holy war id="opinion">
Whenever you need a elseif, then it's time to consider a switch -- like
thus:
switch is fine if your elseif comparisons are equality based. If they're not
equality based, then they don't map to switch as well.
No.7 | | 495 bytes |
| 
Larry Garfield wrote:
switch is fine if your elseif comparisons are equality based. If they're not
equality based, then they don't map to switch as well.
In other words, if you look at a logical ladder as the roots of the
tree, as long as each root has the same number of forks (say each fork
ends only one way), your fine with a switch. If you have one, however,
that has only one condition, and another that has two, then you need an
ifelseif logic tree.
No.8 | | 848 bytes |
| 
At 11:07 PM -0600 6/29/06, John Meyer wrote:
>Larry Garfield wrote:
>>
>>switch is fine if your elseif comparisons are equality based. If they're not equality based, then they don't map to switch as well.
>
>In other words, if you look at a logical ladder as the roots of the tree, as long as each root has the same number of forks (say each fork ends only one way), your fine with a switch. If you have one, however, that has only one condition, and another that has two, then you need an ifelseif logic tree.
Interesting -- can you give me an example?
Perhaps I'm showing my ignorance, but I never ran into an elseif problem that I couldn't better solve (for me) with a switch. I've never *had* to use one.
tedd
No.9 | | 1357 bytes |
| 
6/30/06, tedd <
tedd (AT) sperling (DOT) comwrote:
At 11:07 PM -0600 6/29/06, John Meyer wrote:
>
Larry Garfield wrote:
>
>
>
>
switch is fine if your elseif comparisons are equality based. If they're not equality based, then they don't map to switch as well.
>
Not true. I've come to really appreciate the structure of switches.
What helps to "unlock" their power is that you don't *have* to compare
your switch() param with your case() params.
Building on Paul's good advice of splitting logic from presentation:
Code:
$display_name = '';
switch('set_display_name'){case($row[1] == 'none'):case($row[1] == $row[2]):$display_name = $row[0] . ' ' . $row[2];
break;
default:$display_name = $row[0] . ' (' . $row[1] . ') ' . $row[2];
break;
}// now print table, using $display_name
HTH,
John W
No.10 | | 386 bytes |
| 
Thu, June 29, 2006 11:07 pm, Larry Garfield wrote:
switch is fine if your elseif comparisons are equality based. If
they're not
equality based, then they don't map to switch as well.
Except in PHP which supports:
switch(TRUE) {
case _boolean_expression_:
break;
}
So you can have case statements of whatever complexity is appropriate.
Re: Multiple "if()" statements