Could somebody please set me straight here?
I am converting our Community Resource Directory from static pages to PHP/MySQL.
When pulling records from MySQL, I need to loop past empty fields WITHOUT printing anything as there are occasions when some of the fields have no entry. Fields that could be empty are set to NULL in MySQL.
I have tried many variations of the coding below and searched many archives for previous instances of this problem, but have drawn a blank. Could be using wrong keywords.
Obviously, I am on the wrong track here and would be eternally grateful for any help.
CODING:
$result = @("SELECT OrgName, Contact1Name, Contact1Phone1, Contact1Phone2, Contact1Mobile, Contact1Fax, Contact1Email, Contact2Name, Contact2Phone1, Contact2Phone2, Contact2Mobile, Contact2Fax, Contact2Email, PostalAddress, StreetAddress, WebSite, HoursAvail, Description FROM dircontent WHERE CatName = 'Accommodation' ORDER BY OrgName ASC");
if (!$result) {
echo ( "<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
// Display header in h tags and the details of each organisation in a paragraph
if ($row = mysql_fetch_array($result) ) {
$orgname = $row["OrgName"];
$contact1name = $row["Contact1Name"];
$contact1phone1 = $row["Contact1Phone1"];
$contact1phone2 = $row["Contact1Phone2"];
$contact1mobile = $row["Contact1Mobile"];
$contact1fax = $row["Contact1Fax"];
$contact1email = $row["Contact1Email"];
$contact2name = $row["Contact2Name"];
$contact2phone1 = $row["Contact2Phone1"];
$contact2phone2 = $row["Contact2Phone2"];
$contact2mobile = $row["Contact2Mobile"];
$contact2fax = $row["Contact2Fax"];
$contact2email = $row["Contact2Email"];
$postaladdress = $row["PostalAddress"];
$streetaddress = $row["StreetAddress"];
$website = $row["WebSite"];
$hoursavail = $row["HoursAvail"];
$description = $row["Description"];
do {
printf("<h4>%s</h4>", $row["OrgName"]);
if(isset($contact1name)) {printf("<p class=\"medtextver\">%s<br>", $row["Contact1Name"]);}
if(isset($contact1phone1)) {printf("Phone: %s<br>", $row["Contact1Phone1"]);}
if(isset($contact1phone2)) {printf("Phone: %s<br>", $row["Contact1Phone2"]);}
if(isset($contact1mobile)) {printf("Mobile: %s<br>", $row["Contact1Mobile"]);}
if(isset($contact1fax)) {printf("Fax: %s<br>", $row["Contact1Fax"]);}
if(isset($contact1email)) {printf("Email: <a href=\"mailto:". $row["Contact1Email"] ."\" class=\"medtextver\">%s</a><br>", $row["Contact1Email"]);}
if(isset($contact2name)) {printf("%s<br>", $row["Contact2Name"]);}
if(isset($contact2phone1)) {printf("Phone: %s<br>", $row["Contact2Phone1"]);}
if(isset($contact2phone2)) {printf("Phone: %s<br>", $row["Contact2Phone2"]);}
if(isset($contact2mobile)) {printf("Mobile: %s<br>", $row["Contact2Mobile"]);}
if(isset($contact2fax)) {printf("Fax: %s<br>", $row["Contact2Fax"]);}
if(isset($contact2email)) {printf("Email: <a href=\"mailto:". $row["Contact2Email"] ."\" class=\"medtextver\">%s</a><br>", $row["Contact2Email"]);}
if(isset($postaladdress)) {printf("%s<br>", $row["PostalAddress"]);}
if(isset($streetaddress)) {printf("%s<br>", $row["StreetAddress"]);}
if(isset($website)) {printf("Web Site: <a href=\"http://". $row["WebSite"] ."\" class=\"medtextver\" target=\"_blank\">%s</a><br>", $row["WebSite"]);}
if(isset($hoursavail)) {printf("%s<br>", $row["HoursAvail"]);}
if(isset($description)) {printf("<i>%s</i></p>", $row["Description"]);}
}
while ($row = mysql_fetch_array($result) ) ;
echo "<p>"; // don't really need this
}
else {
echo "<p class=\"medtextver\">Sorry, no records were found!</p>";
}
END CODING
The above produces the following, which is obviously unsatisfactory when there are many entries on a page for display:
GARDEN VILLAS
Reception
Phone: 4744 5100
Phone:
Mobile:
Fax: 4744 5105
Email:
Phone:
Phone:
Mobile:
Fax:
Email: lauraj@obe.com.au
P.O. Box 958, Mount Isa, QLD. 4825
3 Lucy St., Mount Isa, QLD. 4825
Web Site: www.lauraj.com.au
8am - 5pm
Retirement accommodation units for rental.
Old age is probably preventing me from being able to absorb PHP to the extent I desire ;-)