white space from empty datafields when echo-ing?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
leathersmith
Forum Newbie
Posts: 5
Joined: Sun Jul 13, 2003 6:45 am
Location: Ft Lauderdale Florida

white space from empty datafields when echo-ing?

Post by leathersmith »

Not that I can right a snippet of code (yet) but am great at copy & paste!!
............
My first project with PHP & MySQL is a health resources directory; currently am able to display database contents very nicely except I get lots of white space when fields are empty. For example, some resource facilities have one address, others 2, one place has 4, some have 1 or 2 phone numbers, etc. I am just echoing everything, blank or not!

Looks like crap really....I probably need some kind of IF thingie going on?

I have 40 separate pages of resource types (housing, food, financial, etc etc) and use INCLUDE in each so i now only have to modify one itty bitty page of php, and would appreciate any suggestions (esp. those that involve copy&paste! ........kidding!!!!)

here it is:

<?php
// Display
while ( $row = mysql_fetch_array($result) ) {
echo("<font size=+'1'><b>" . $row["name"] . "</b></font><hr color='blue'>");
echo($row["description1"] . "<p>");
echo($row["description2"] . "");
echo("<table><tr><td align='left' valign='top'>" . $row["address1"] . "");
echo("<br>" . $row["city1"] . "");
echo("<br>" . $row["state1"] . "");
echo("<br>" . $row["zip1"] . "</td>");
echo("<td align='left' valign='top'>" . $row["address2"] . "");
echo("<br>" . $row["city2"] . "");
echo("<br>" . $row["state2"] . "");
echo("<br>" . $row["zip2"] . "</td></tr>");
echo("<tr><td align='left' valign='top'>" . $row["address3"] . "");
echo("<br>" . $row["city3"] . "");
echo("<br>" . $row["state3"] . "");
echo("<br>" . $row["zip3"] . "</td>");
echo("<td align='left' valign='top'>" . $row["address4"] . "");
echo("<br>" . $row["city4"] . "");
echo("<br>" . $row["state4"] . "");
echo("<br>" . $row["zip4"] . "</td></tr></table>");
echo("<br>phone1: " . $row["phone1"] . "");
echo("<br>phone2: " . $row["phone2"] . "");
echo("<br>TDD/TTY: " . $row["tdd"] . "");
echo("<br>fax: " . $row["fax"] . "<p>");
echo($row["times"] . "");
echo("<br>Payments Accepted: " . $row["payments"] . "<p><hr><a href='../resources.html'><i>Back to Resource Directory</i></a><p>");
}
?>

This is displayed at http://www.pwa.org Support -> Resources -> pick a category

Thanks in advance for taking a noob by the hand!
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

You should add an if(isset($row[whatever]){then.. exec code }else{ dont do anything} type thing.

Example:

Code: Select all

<?php
if (isset($row["city1"])){
  echo "<br>" . $row["city1"]; 
} else {
  echo "<br>";
}
?>
Reference material:
http://php.net/isset

BTW: It's also a good idea to trim() the information that goes into the DB so that spaces don't fool this snipplet.
User avatar
leathersmith
Forum Newbie
Posts: 5
Joined: Sun Jul 13, 2003 6:45 am
Location: Ft Lauderdale Florida

Post by leathersmith »

Thanks for responding Sami!

I'll give it a whack! :D
User avatar
leathersmith
Forum Newbie
Posts: 5
Joined: Sun Jul 13, 2003 6:45 am
Location: Ft Lauderdale Florida

its either expanses of white, or run-on sentences....

Post by leathersmith »

achk my brain hurts and its not even noon....

you said:

if (isset($row["city1"])){
echo "<br>" . $row["city1"];
} else {
echo "<br>";
}

which I pasted all over creation, and I guess the else echo <br> was giving me empty white lines so i did:

//address2
if (isset($row["address2"])){
echo "" . $row["address2"];
} else {
echo "";
}

if (isset($row["city2"])){
echo "" . $row["city2"];
} else {
echo "";
}

if (isset($row["state2"])){
echo "" . $row["state2"];
} else {
echo "";
}

if (isset($row["zip2"])){
echo "" . $row["zip2"] . "";
} else {
echo "";
}

which doesn't write <br>'s into the HTML but if there is an address2, city2, state2, zip2 they are all scrunched up on the same line. When I put &nbsp; in between city, state & zip these also write to the HTML I think. But at least they're all on an invisible (white) row, huh?

Can I use \n (i think it is) to define the end of a row and force a next row without having tags show up in my HTML?

I looked at php.net/manual on empty() and trim() but I'm totally in over my head!
?>
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

Post by macewan »

<br> drop you to the next line.

blah1blah2

adding <br> between them does this

blah1
blah2

you just have to decide where they are needed.
User avatar
leathersmith
Forum Newbie
Posts: 5
Joined: Sun Jul 13, 2003 6:45 am
Location: Ft Lauderdale Florida

Post by leathersmith »

Code: Select all

<?php
if (isset($row["city1"])){ 
  echo "<br>" . $row["city1"]; 
} else { 
  echo "<br>"; 
} 


?>
the pesky else <br> insists on showing up even when the isset($row[]) is actually empty

I have many blank fields in the database in some places, and am trying to pretty-up the results, get rid of all the dumb <br>'s that keep echoing.

empty() and trim() confuse me so far; reading the tutes ain't sinking in yet!

I'm getting the line breaks and spaces (ampersand nbsp;) working nicely, where I want them like after address line, and between city space state space zip

but even when address #2, #3 and #4 are null all the tags in the "if" write; alternatively if 2,3 and 4 are empty, I don't want 3 breaks between address1 and phone, just creates lots of white space that looks pathetic
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

Post by macewan »

*note: the caps below are not meant to suggest shouting*

the reason you have <br> is because you tell it to <br>.

Code: Select all

<?php
if (isset($row&#1111;"city1"]))&#123;
  echo "<br>" . $row&#1111;"city1"];
&#125; else &#123;
  echo "<br>";   // <- IT IS SHOWING UP BECAUSE YOU TELL IT TO
&#125;
?>
Post Reply