gerb Posted June 20, 2007 Share Posted June 20, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/56460-how-to-not-leave-a-blank-line-when-echoing-null-fields-during-a-while-loop/ Share on other sites More sharing options...
epic_era Posted June 20, 2007 Share Posted June 20, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/56460-how-to-not-leave-a-blank-line-when-echoing-null-fields-during-a-while-loop/#findComment-278861 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.