Jump to content

How to not leave a blank line when echoing null fields during a while loop.


gerb

Recommended Posts

Just registered today, and am very new to PHP.  This is very difficult to explain, which is probably also why I have had difficulty finding help for this item.  I imagine the fix will be very simple, I will try to explain.  When I do a mysql_query and have a while loop to display the information in HTML I want the results displayed each on a new line, and each result may also have one or two pictures displayed.  However, if a result does not have data in one of the fields, I don't want the blank line to show in HTML.

 

Kind of like this:

 

Widget Company

John Doe

Address

City, State, Zip

 

If a persons name is not present, I want it to show like this:

 

Widget Company

Address

City, State, Zip

 

not like this:

 

Widget Company

                  < (empty space)

Address

City, State, Zip

 

Same is true for the picture.  If a name is not present in the database for the picture, I do not want the empty image box to show in HTML.

 

Here is the portion of my code. (I have probably got this way goofed up!)

 

Thanks for any help.  Gerb...

 

$bizcards = @mysql_query($select . $from . $where);

if (!$bizcards) {

    exit('<p>Error retrieving bizcards from database!<br />'.

      'Error: ' . mysql_error() . '</p>');

}

 

while ($bizcard = mysql_fetch_array($bizcards)) {

  $bizname = htmlspecialchars($bizcard['bizname']);

  $nameoncard = htmlspecialchars($bizcard['nameoncard']);

  $city = htmlspecialchars($bizcard['city']);

  $state = htmlspecialchars($bizcard['state']);

  $postalcode = htmlspecialchars($bizcard['postalcode']);

  $county = htmlspecialchars($bizcard['county']);

  $phone = htmlspecialchars($bizcard['phone']);

  $cardfront = ($bizcard['cardfront']);

  $cardback = ($bizcard['cardback']);

?>

<hr width=75% size=4>

<p align="center"><strong><?php echo "$bizname"; ?></strong><br />

<?php if ($nameoncard) {echo "$nameoncard \n";} ?><br />

<?php echo "$city $state $postalcode"; ?><br />

<?php echo "$phone"; ?><br />

<img  src="http://localhost/<?php echo $cardfront; ?>.jpg" width="350" height="200" alt="Card Image"><br />

<img  src="http://localhost/<?php echo $cardback; ?>.jpg" width="350" height="200" alt="Card Image"></p>

<?php

}

?>

 

<hr width=75% size=4>

<p align="center"><?php if (!$bizname)

{echo "No Businesses Meet The Search Criteria; Select New Search to Try Again";} ?></p>

 

<p align="center"><a href="index.php">New Search</a></p>

</body>

</html>

 

 

 

what you need to do is to verify that the data that you are about to display exists kinda like what you do here:

<?php if ($nameoncard) {echo "$nameoncard \n";} ?>

 

the same must be done with all the others:

 

 

<?php if($city && $state && $postalcode){echo "$city $state $postalcode";} ?>

 

and so on....

hope it helps

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.