return a value if field is not empty

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
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

return a value if field is not empty

Post 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
zyntrax
Forum Commoner
Posts: 32
Joined: Wed Apr 13, 2011 2:23 am
Location: Sweden

Re: return a value if field is not empty

Post by zyntrax »

try this:

<?php
if($row_Cal_list['entry_form'] != '')
{
echo "<a href='<?php echo $row_Cal_list['entry_form']; ?>'>Entry Form</a>";
}
?>
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

Re: return a value if field is not empty

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: return a value if field is not empty

Post 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>';
}
?>
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

Re: return a value if field is not empty

Post by Alpal »

Thankyou, will have to learn more about php
Your assistance is greatly appreciated.
Post Reply