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

5

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>";
}
Sign up to request clarification or add additional context in comments.

3 Comments

empty() is the function you were looking for.
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.
@KipShaw I'm very glad to help person like you. Keep it up! ;)
1
while($field = mysql_fetch_array($result))
{
    echo "<h3>".$field['business']."</h3>";
    if ($field['website'] != '') {
        echo "<p>Website: ".$field['website']."</p>";
    }
}

Comments

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>";
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.