Forum Moderators: coopster
<?
$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
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.
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
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>"; }
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! ;)
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
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!= "")?
Thanks again.
fiddler