0

I've got a client who wants me to format a list of businesses on a website page using data from a mysql database and putting it on the web page using php.

The client wants each piece of data to be identified, like this:

Contact Person: Sue Smith
Website: greatwebsite.com

Here's the problem:

Some of the businesses don't have a website. So I do NOT want the business listing to show up like this:

Contact Person: Sue Smith
Website:

I do NOT want the Website line to show up at all if there is no website.

Here's what I've done so far - which does NOT solve the problem (truncated for brevity):

$result = mysql_query("SELECT * FROM businesses ORDER BY business");
while($field = mysql_fetch_array($result))
{
    echo
    "<h3>".$field['business']."</h3>
    <p>Website: ".$field['website']."</p>";
}

I need to learn how to delete the "Website" line entirely if there is no website.

3 Answers 3

6

A simple if will work fine:

$result = mysql_query("SELECT * FROM businesses ORDER BY business");
while($field = mysql_fetch_array($result))
{
   if (! empty($field['business']) )
      echo "<h3>".$field['business']."</h3>";

   if (! empty($field['website']) )
      echo "<p>Website: ".$field['website']."</p>";
}
3
  • 1
    empty() is the function you were looking for.
    – Yes Barry
    Nov 1, 2011 at 21:18
  • 1
    To all who answered me promptly, I offer a grateful THANKS! To you who are further along, you have helped a newbie immensely. This is what makes this Internet community so powerful. Again, thanks.
    – Kip Shaw
    Nov 1, 2011 at 23:31
  • 1
    @KipShaw I'm very glad to help person like you. Keep it up! ;) Nov 1, 2011 at 23:57
1
while($field = mysql_fetch_array($result))
{
    echo "<h3>".$field['business']."</h3>";
    if ($field['website'] != '') {
        echo "<p>Website: ".$field['website']."</p>";
    }
}
0
$result = mysql_query("SELECT * FROM businesses ORDER BY business");
while($field = mysql_fetch_array($result))
{
    echo "<h3>".$field['business']."</h3>";
    if(!empty($field['website']))
        <p>Website: ".$field['website']."</p>";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.