Forum Moderators: coopster

Message Too Old, No Replies

php script - results show unwanted blank line

         

fiddler

7:15 pm on May 19, 2005 (gmt 0)

10+ Year Member



I have a script for a address book that I need to tweak. Some members have a physical address (Address1) and a mailing address (Address2), but some members have only one address.
With the script I am using, for those members with only 1 Address and not two, the second "address line" comes up as a blank line. I would like to delete this line altogether but have so far been unable to figure out how to do it.
The script I am using which brings up the empty line is below:

<?

$db_name ="noodle";

$table_name ="Member_Directory";

$connection = @mysql_connect("localhost","noodle","puffballs") or die(mysql_error());

$db = @mysql_select_db($db_name,$connection) or die(mysql_error());

$sql = "SELECT `Letter`,`Name`,`Address1`,`Address2`,`City`,`County`,`State`,`Zip`,`Phone`,`Fax`,`Brands`,`email`,`WebSite` FROM `Members_Directory` ORDER BY 'Name' ";

$result = @mysql_query($sql,$connection) or die(mysql_error());

while ($row = mysql_fetch_array($result)) {

$Name = $row['Name'];
$Address1 = stripslashes($row['Address1']);
$Address2 = stripslashes($row['Address2']);
$City = stripslashes($row['City']);
$County = stripslashes($row['County']);
$State = stripslashes($row['State']);
$Zip = stripslashes($row['Zip']);
$Phone = $row['Phone'];
$Fax = $row['Fax'];
$Brands = stripslashes($row['Brands']);
$email = $row['email'];
$WebSite = $row['WebSite'];

$display_block .= "<P><strong>$Name</strong><br>
$Address1 <br>
$Address2 <br>
$City, $State $Zip<br>
$County <br>
Phone: $Phone<br>
Fax: $Fax<br>
$Brands<br>
<a href=\"mailto:$email\">$email</a><br>
<a href=\"http://$WebSite\">$WebSite</a></P>";

}
?>

<HTML>
<HEAD>
<TITLE>Members List (Ordered by Name)</TITLE>
</HEAD>
<BODY>
<H2>Members List:Ordered by Name</H2>
<? echo "$display_block";?>

</BODY>
</HTML>

I have an additional script that will take out the blank line, but creates another problem in the process, that being it only shows the last member, none of the other members are acknowledged. The script, below, is entered in place of the <? echo "$display_block";?>
which follows the <H2>Members List:Ordered by Name</H2> in the Body of the file.

script:
<?

$db_name ="noodle";

$table_name ="Member_Directory";

$connection = @mysql_connect("localhost","noodle","puffballs") or die(mysql_error());

$db = @mysql_select_db($db_name,$connection) or die(mysql_error());

$sql = "SELECT

`Letter`,`Name`,`Address1`,`Address2`,`City`,`County`,`State`,`Zip`,`Phone`,`Fax`,`Brands`,`email

`,`WebSite` FROM `Members_Directory` ORDER BY 'Name' ";

$result = @mysql_query($sql,$connection) or die(mysql_error());

while ($row = mysql_fetch_array($result)) {

$Name = $row['Name'];
$Address1 = stripslashes($row['Address1']);
$Address2 = stripslashes($row['Address2']);
$City = stripslashes($row['City']);
$County = stripslashes($row['County']);
$State = stripslashes($row['State']);
$Zip = stripslashes($row['Zip']);
$Phone = $row['Phone'];
$Fax = $row['Fax'];
$Brands = stripslashes($row['Brands']);
$email = $row['email'];
$WebSite = $row['WebSite'];

}
?>

<HTML>
<HEAD>
<TITLE>Members List (Ordered by Name)</TITLE>
</HEAD>
<BODY>
<H2>Members List:Ordered by Name</H2>
<P><strong><? echo "$Name";?></strong> <br>
<?
if ($Address1!="") {
echo "$Address1 <br>";
}
if ($Address2!="") {
echo "$Address2 <br>";
}
?>
<? echo "$City, $State $Zip";?> <br>

<?
if ($County!="") {
echo "$County <br>";
}
if ($Phone!="") {
echo "Phone: $Phone <br>";
}
if ($Fax!="") {
echo "Fax: $Fax <br>";
}
if ($Brands!="") {
echo "$Brands <br>";
}
if ($email!="") {
echo "<a href=\"mailto:$email\">$email</a><br>";
}
if ($WebSite!="") {
echo "<a href=\"http://$WebSite\">$WebSite</a><br>";
}

?>
</P>

</BODY>
</HTML>

I've looked that the table very carefully to make sure no unwanted spaces appear to alter the way the info should be displayed and everything appears fine there...

Anyone know why this second script - although listing the info in the manner I want - only brings up the last member and not everyone?....and is there any suggestion on how to correct this?

Thanks muchly for your time.
fiddler

StupidScript

7:23 pm on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about:

if (!$row['Address2']) { $Address2=""; }

else { $Address2 = stripslashes($row['Address2'])."<br>\n"; }

Build the line break into the variable so it won't muck up your display if it doesn't exist.

In the second example, you are ending up with only the LAST record, because you are looping and overwriting the values from the previous record ... then printing the output ... which is the final overwrite: the last record.

fiddler

7:28 pm on May 19, 2005 (gmt 0)

10+ Year Member



uh, just tried that - and it made the blank space wider - as if it put in two blank lines now instead of one.

fiddler

7:31 pm on May 19, 2005 (gmt 0)

10+ Year Member



Dumb question here -

in your comments...
In the second example, you are ending up with only the LAST record, because you are looping and overwriting the values from the previous record ... then printing the output ... which is the final overwrite: the last record.

how do I stop it from looping and overwriting - what do I need to change, that is (I'm about as novice to php as one can get).

Thanks for taking a look, btw.
fiddler

StupidScript

9:34 pm on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm going to stick with your first example, as it shows what is being done correctly, i.e. for each
while
loop you not only gather the data but also collect the code that displays, adding new lines with each loop. (The second example runs through the loop until it's done looping and then it writes only one row ... the last record).

while ($row = mysql_fetch_array($result)) { 

if (!$row["Name"]) { $Name=""; }

else { $Name = "<strong>".$row['Name']."</strong><br />\n"; }

if (!$row["Address1"]) { $Address1=""; }

else {$Address1 = stripslashes($row['Address1'])."<br />\n"; }

if (!$row["Address2"]) { $Address2=""; }

else {$Address2 = stripslashes($row['Address2'])."<br />\n"; }

if (!$row["City"]) { $City="<b>No City</b>, "; }

else { $City = stripslashes($row['City']).", "; }

if (!$row["County"]) { $County=""; }

else { $County = stripslashes($row['County'])."<br />\n"; }

if (!$row["State"]) { $State="<b>No State</b> "; }

else { $State = stripslashes($row['State'])." "; }

if (!$row["Zip"]) { $Zip="<br />\n"; }

else { $Zip = stripslashes($row['Zip'])."<br />\n"; }

if (!$row["Phone"] { $Phone=""; }

else { $Phone = "Phone: ".$row['Phone']."<br />\n"; }

if (!$row["Fax"]) { $Fax=""; }

else { $Fax = "Fax: ".$row['Fax']."<br />\n"; }

if (!$row["Brands"]) { $Brands=""; }

else { $Brands = stripslashes($row['Brands'])."<br />\n"; }

if (!$row["email"]) { $email=""; }

else { $email = "<a href=\"mailto:".$row['email']."\">".$row['email']."</a><br />\n"; }

if (!$row["WebSite"] { $WebSite=""; }

else { $WebSite = "<a href=\"http://".$row['WebSite']."\">".$row['WebSite']."</a>\n"; }

$display_block .= "<p>$Name $Address1 $Address2 $City $State $Zip $County $Phone $Fax $Brands $email $WebSite</p>"; 

}

fiddler

6:28 pm on May 20, 2005 (gmt 0)

10+ Year Member



Forgive my denseness, StupidScript, and thank you for your time concerning my problem.....uh, but when I applied the script above, it made no dif - the blank line in lieu of Address2 still shows up.
fiddler

StupidScript

9:41 pm on May 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then Address2 must not be empty. There's probably a spacebar or something in there. You may need to change your validation routine to something like:

if ((!$row["Address2"])¦¦(strlen($row["Address2"])<3)) { $Address2=""; }

checking to see if $row["Address2"] is completely empty or if it's less than 3 chars in length.

The same applies to the other validation routines.

(No denseness going on ... just sneaky little buggers! ;)

fiddler

11:20 am on May 23, 2005 (gmt 0)

10+ Year Member



I thought of that, StupidScript, so exported the column to check for extra spaces - I even edited the column via phpMyAdmin to delete any extra spaces and although nothing showed up I ran through the motions of pressing the backspace and delete keys several times to make sure and still there were blank lines popping up for the same names I had edited.

I guess I am confused here, but I am wondering why - with the script that looping/overwriting action going on - the last entry shows up correctly, with no blank lines. Yet it comes up with blank lines with your suggestions the other way.

Is there a way that you (or anyone) can show me how to stop that looping/overwriting in the second script? I've looked through the manuals, but everything I've tried didn't seem to do the trick. Clearly I'm missing something.

fiddler

StupidScript

4:50 pm on May 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi fiddler.

You need to include the writing of the data in the getting of the data loop.

# START GETTING DATA WHILE LOOP

while($row=mysql_fetch_array($getdata)) {

$Name=$row["Name"];

...etc...

echo "<strong>".$Name."</strong><br />\n";

...etc...

# END GETTING DATA WHILE LOOP

}

so each loop (a) gets the current record AND (b) echos it.

Your second example is like:

# START GETTING DATA WHILE LOOP

while($row=mysql_fetch_array($getdata)) {

$Name=$row["Name"];

...etc...

# END GETTING DATA WHILE LOOP

}

echo "<strong>".$Name."</strong><br />\n";

...etc...

so it ends up with only the last loop's data ... then you echo that result one time.

Have you tried changing my

(!$Name)
to your
($Name!= "")
?

fiddler

11:18 am on May 26, 2005 (gmt 0)

10+ Year Member



Hey, StupidScript.
Thanks so much for your valueable time and info - I tried your script that you send on May 19 again - there were a couple of ) missing that I was able to find and put back in) - and don't ask me why it wouldn't work before, but it works now, just the way it is suppose to. Couldn't have gotten this without your help.

Thanks again.
fiddler

StupidScript

4:42 pm on May 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops! I see 'em on Phone and Zip ... sorry 'bout that. Too lazy to test, I guess.

You can find lots of skilled folks here that would have taken over if I crapped out. But thanks for the thanks! ;)

Enjoy!