hidefind hidefind - 2 months ago 6x
PHP Question

PHP how to use if $var['table'] else if ['table'] is null to display blank

I have a database that returns results based on query:

<?php foreach ($data as $key=> $item) { ?>
<td class="text-left"><?php echo ucwords(strtolower($item['biz_name'])); ?></td>
<td class="text-left"><?php echo '<a href="http://maps.google.com/?q='. $item['loc_LAT_centroid'] .','. $item['loc_LONG_centroid'] .'">'.ucwords(strtolower($item['e_address'])).'</a>'; ?></td>
<td class="text-left"><?php echo ucwords(strtolower($item['e_city'])); ?></td>
<td class="text-center"><?php echo strtoupper($item['e_state']); ?></td>
<td class="text-center"><?php echo $item['e_postal']; ?></td>
<td class="text-center"><?php echo $item['biz_phone']; ?></td>
<?php if (empty($item['web_url'])) {
<td class="text-center"><?php echo ''; ?></td>
else {
<td class="text-center"><?php echo '<a href="'. $item['web_url'] .'" >'."View Site".'</a>'; ?></td>
} ?>

I'm obviously doing this wrong but this is what I've pieced together so far. What I'm trying to do is have the web_url table field display blank if the value is null, otherwise display "View Site" if it contains a value. I'm fairly new to php so any help is appreciated.

I get a parse error on this line presently:

<?php if (empty($item['web_url'])) {


You should use a ternary operator in that case:

<td class="text-center">
    <?php echo (isset($item['web_url']) && !empty($item['web_url'])) ? ('<a href="'. $item['web_url'] .'" >'."View Site".'</a>') : ''; ?>