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!
white space from empty datafields when echo-ing?
Moderator: General Moderators
- leathersmith
- Forum Newbie
- Posts: 5
- Joined: Sun Jul 13, 2003 6:45 am
- Location: Ft Lauderdale Florida
You should add an if(isset($row[whatever]){then.. exec code }else{ dont do anything} type thing.
Example:
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.
Example:
Code: Select all
<?php
if (isset($row["city1"])){
echo "<br>" . $row["city1"];
} else {
echo "<br>";
}
?>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.
- leathersmith
- Forum Newbie
- Posts: 5
- Joined: Sun Jul 13, 2003 6:45 am
- Location: Ft Lauderdale Florida
- 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....
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 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!
?>
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 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!
?>
- leathersmith
- Forum Newbie
- Posts: 5
- Joined: Sun Jul 13, 2003 6:45 am
- Location: Ft Lauderdale Florida
Code: Select all
<?php
if (isset($row["city1"])){
echo "<br>" . $row["city1"];
} else {
echo "<br>";
}
?>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
*note: the caps below are not meant to suggest shouting*
the reason you have <br> is because you tell it to <br>.
the reason you have <br> is because you tell it to <br>.
Code: Select all
<?php
if (isset($rowї"city1"])){
echo "<br>" . $rowї"city1"];
} else {
echo "<br>"; // <- IT IS SHOWING UP BECAUSE YOU TELL IT TO
}
?>