Page 1 of 1

return a value if field is not empty

Posted: Wed Apr 20, 2011 5:48 am
by Alpal
Would appreciate some assistance
Have pdf's uploaded to a database, want the user to be able to download/open them. Not a problem
<a href="<?php echo $row_Cal_list['entry_form']; ?>">Entry Form</a>
Works fine

Problem, I only want the link to show if there is an entry form available, if the field is empty then I would prefer it to be blank
Have this which works nicely.
<?php
if($row_Cal_list['entry_form'] != '')
{
echo $row_Cal_list['entry_form'];
}
?>
How do I put the two together, or is there a better way?
Am a dreamweaver user and obviously am no genius where php is concerned
Thanks in advance

Re: return a value if field is not empty

Posted: Wed Apr 20, 2011 8:48 am
by zyntrax
try this:

<?php
if($row_Cal_list['entry_form'] != '')
{
echo "<a href='<?php echo $row_Cal_list['entry_form']; ?>'>Entry Form</a>";
}
?>

Re: return a value if field is not empty

Posted: Wed Apr 20, 2011 4:53 pm
by Alpal
Thanks
Nice try, but does not work
Error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
any other ideas?

Thanks in advance

Re: return a value if field is not empty

Posted: Wed Apr 20, 2011 5:40 pm
by McInfo
Some valid syntax to choose from:

Code: Select all

<?php if ($row_Cal_list['entry_form'] != ''): ?>
<a href="<?php echo $row_Cal_list['entry_form']; ?>">Entry Form</a>
<?php endif; ?>

<?php if ($row_Cal_list['entry_form'] != '') { ?>
<a href="<?php echo $row_Cal_list['entry_form']; ?>">Entry Form</a>
<?php } ?>

<?php if ($row_Cal_list['entry_form'] != '') {
    ?><a href="<?php echo $row_Cal_list['entry_form']; ?>">Entry Form</a><?php
} ?>

<?php
if ($row_Cal_list["entry_form"] != "") :
    echo '<a href="', $row_Cal_list['entry_form'], '">Entry Form</a>';
    printf('<a href="%s">Entry Form</a>', $row_Cal_list['entry_form']);
    echo sprintf("<a href=\"%s\">Entry Form</a>", $row_Cal_list['entry_form']);
endif;
?>

<?php
if ($row_Cal_list['entry_form'] != '') {
    echo "<a href=\"$row_Cal_list[entry_form]\">Entry Form</a>";
    echo "<a href=\"{$row_Cal_list['entry_form']}\">Entry Form</a>";
    echo "<a href=\"" . $row_Cal_list['entry_form'] . "\">Entry Form</a>";
    echo '<a href="' . $row_Cal_list['entry_form'] . '">Entry Form</a>';
}
?>

Re: return a value if field is not empty

Posted: Wed Apr 20, 2011 7:06 pm
by Alpal
Thankyou, will have to learn more about php
Your assistance is greatly appreciated.