Page 1 of 1

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

Posted: Thu May 22, 2003 2:11 pm
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!!!

Posted: Thu May 22, 2003 2:14 pm
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...

Posted: Thu May 22, 2003 9:19 pm
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'; 
}