buni
buni

Reputation: 662

Show no link for empty data in Mysql and php

<td><?php echo "<a href=\"".$row['link']."\" target=\"_self\">Link</a>"; ?></td>

I would like the Link to be hyper-linked only if there is a data['link']in MySQL table

Any idea?

Upvotes: 3

Views: 400

Answers (3)

Nasreddine
Nasreddine

Reputation: 37838

<?php
    if(isset($row['link']) && !empty($row['link']))
    {
        echo '<td><a href="'.$row['link'].'" target="_self\">Link</a></td>';
    } else {
        echo '<td>Link</td>';
    }
?>

Upvotes: 1

abelito
abelito

Reputation: 1104

Do an isset and trim + strlen check of 0 around the data, before wrapping the Link caption in an anchor tag.

$linkHtml = "Bloopdie";

// Add anchor tag only if there is data in the row
if (isset($row['data']) && strlen(trim($row['data'])) > 0) {
  $linkHtml = '<a href="' . $row['data'] . '">' . $linkHtml . '</a>';
}

echo $linkHtml

Upvotes: 0

Jake N
Jake N

Reputation: 10583

Without knowing the structure of your table, it's not possible to say if this will work, but it is a good start:

<?php if(strlen($row['link']) > 0):?>
<td><?php echo "<a href=\"".$row['link']."\" target=\"_self\">Link</a>"; ?></td>
<?php endif; ?>

Upvotes: 1

Related Questions