Nesting an "if" statement inside a "printf&qu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ibanezrg770
Forum Newbie
Posts: 4
Joined: Tue May 20, 2003 9:05 pm

Nesting an "if" statement inside a "printf&qu

Post by ibanezrg770 »

Hi, I am using a MYSQL_FETCH_ARRAY and PRINTF satement to pull data and place it into a table. One of the fields is an email field that may or may not have emails. If they do have an email address supplied, I want the table to display a link with the HTML code "HREF=MAILTO:'EMAILADDRESS' , if not, I just want it to display "not entered". Can I nest an "IF" statement inside a "PRINTF" statement. So far, the HTML reads the "IF" statement as characters, and not code. Is there a way to do this or must I try another way by doing a search before the "MYSQL_FETCH_ARRAY"? Thanks in advance. Here is what the code looks like now...

<?php
//List Rows
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
printf ("
<tr><td width='200'><font color='#FF0000'size='-1'>%s</td>
<td width='200'><font color='#FF0000'size='-1'>%s</td>
<td width='200'><font color='#FF0000'size='-1'>%s</td>
<td width='200'><font color='#FF0000'size='-1'>
[if ($row[3] == 'not entered')
{
echo 'Not Entered';
}
else
{
echo '<a href=mailto:{$row[3]}>Email Me';
}]
</td>
<td width='200'><font color='#FF0000'size='-1'><a href='$row[4]'>My Homepage</td>
<td width='200'><font color='#FF0000'size='-1'>%s</td>
<td width='200'><font color='#FF0000'size='-1'>%s</td><td width='200'><font color='#FF0000'size='-1'>%s</td>
<td width='200'><font color='#FF0000'size='-1'>%s</td><td width='200'><font color='#FF0000'size='-1'>%s</td>
<td width='200'><font color='#FF0000'size='-1'>%s</td>
<td width='200'><font color='#FF0000'size='-1'>%s</td><td width='200'><font color='#FF0000'size='-1'>%s</td>
<td width='200'><font color='#FF0000'size='-1'>%s</td>
<td width='60'><font color='#FF0000'size='-1'>%s</td></tr>",
$row[0], $row[2], $row[1], $row[5], $row[6], $row[7], $row[8], $row[9], $row[10], $row[11], $row[12], $row[13], $row[14]);
}
?>

Timmay!!!
slimsam1
Forum Commoner
Posts: 49
Joined: Wed Aug 21, 2002 12:20 am

Post by slimsam1 »

instead of a printf, use regular print and instead of %s, use $row[0], $row[1], etc...
that's how I would do it. I really don't see why you used printf in the first place...
User avatar
Wayne Herbert
Forum Commoner
Posts: 34
Joined: Tue Apr 29, 2003 3:13 pm
Location: Houston, Texas

Post by Wayne Herbert »

Why not setup the variable to be printed outside the printf, then just print the contents of the variable in the printf as in

Code: Select all

[if ($row[3] == 'not entered') 
{ 
$printemail = 'Not Entered'; 
} 
else 
{ 
$printemail = '<a href=mailto:{$row[3]}>Email Me'; 
}
Post Reply