DoubleA
DoubleA

Reputation: 746

How do I display a row if it is not null and not empty?

I have a page that displays various fields from a mysql_query. I only want to display the following field/row when the value is available.

$web = $row['Website'];


<?php 
if ($web = NULL)
{?>
<!-- DO NOTHING -->
<?php
}
else
{?>
<tr>
<td valign='top' colspan='3' align='center'><DIV CLASS='standings_title'><?php echo $web ?></div></td>
</tr>
<?php
}
?>

Hopefully someone can tell me what I am doing wrong.

Upvotes: 1

Views: 8789

Answers (5)

Maher
Maher

Reputation: 2547

Post column of the row if is not null (in PDO):

    $sql = 'SELECT * FROM posts where post_date !="" ';
    $stmt = $database->prepare($sql);
    $stmt->execute();
    $rowCount = $stmt->rowCount(); 

    if($rowCount < 1)
    {
        return null;
    }
    else
    {
      do somthing ...
    }

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94645

Use isset().

if(isset($web))
{

}

Or

if($web)
{

}

Upvotes: 2

AlexanderZ
AlexanderZ

Reputation: 521

if (isset($value) && strlen($value) > 0) {
  //stuff
}

This is better than using empty($value) or just a if($value) because php considers things such as "0" as empty. However, if you can guarantee that value will never be "0", they are functionally equivalent, and !empty($value) will be faster.

From the php manual:

The following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)

Upvotes: 3

xdazz
xdazz

Reputation: 160833

<?php if($web):?>
<tr>
<td valign='top' colspan='3' align='center'><DIV CLASS='standings_title'><?php echo $web ?></div></td>
</tr>
<?php endif; ?>

Upvotes: 3

Michael Berkowski
Michael Berkowski

Reputation: 270607

$web = $row['Website'];
// Trim whitespace so a single blank space doesn't display as a populated value.
$web = trim($web);
if (!empty($web) {
  // Display it.
}

Upvotes: 0

Related Questions