Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load MySQL Fields into Array - Ignoring blank ones

Status
Not open for further replies.

Muppsy007

Programmer
Apr 4, 2003
98
NZ
Hi there. A bit of a PHP novice, but getting there.

This project I'm working on uses a MySQL DB that contains travel advice to around 200 countries with the following structure:

ID
Country_Name
Date_Updated
Diphtheria
Mening
Yellow
Polio
Typhoid
Malaria
HepatitisA
HepatitisB
Rabies
Tuberculosis
Cholera
Tickborne
Japanese

Text

The above bolded fields contain single line reccommendations for each specific disease, but for a lot of countries, one or more of these fields are blank as there is no risk.

What I'm trying to do maybe not worth the time, but here goes.

I want to start by filling an array with the fields from the database.

Then I want to run through a for statement for the length of the array, and display only the records which aren't empty.

To give you an idea, I can acheive this using multiple if statements as in the following:

[blue]if($resultrow["Diphtheria"]) {
$Text = $Text . "<b>Diphtheria: </b>" . $resultrow["Diphtheria"] . "<br><br>";
}
if($resultrow["Mening"]) {
$Text = $Text . "<b>Meningitus: </b>" . $resultrow["Mening"] . "<br><br>";
}[/blue]

Obviously where the "<b>Diphtheria: </b>" part is I want to replace with a peice of code that displays the field name that it is displaying.

Can anyone point me in the direction of a decent tutorial on filling arrays with database elements and using them in this way. I've found a bit of info on arrays but can't find anything on getting database fields without specifically writing each field into an array.

Thanks
 
Cancel that - I think I worked it out:

[blue]
$advice = array("Diphtheria" => $resultrow["Diphtheria"],
"Meningitus" => $resultrow["Mening"],
"Yellow Fever" => $resultrow["Yellow"],
"Polio" => $resultrow["Polio"],
"Typhoid" => $resultrow["Typhoid"],
"Malaria" => $resultrow["Malaria"],
"Hepatitis A" => $resultrow["HepatitisA"],
"Hepatitis B" => $resultrow["HepatitisB"],
"Rabies" => $resultrow["Rabies"],
"Tuberculosis" => $resultrow["Tuberculosis"],
"Cholera" => $resultrow["Cholera"],
"Tickborne" => $resultrow["Tickborne"],
"Japanese Fever" => $resultrow["Japanese"]);

// Header for the country
printf("<center><h1>");
printf("Travel Advice for " . $resultrow["Country_Name"] . "<br><br>");
printf("</center></h1>");

// Now, for each immunisation advice, if it is not empty - display it.
foreach ($advice as $key => $value) {
if ($value) {
echo("<b>$key:</b> $value<br><br>");
}
}[/blue]

If any one knows of a better way, let me know please.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top