How can nested loops be utilized in PHP to display data in a tabular format for a glossary?
To display data in a tabular format for a glossary using nested loops in PHP, you can iterate over an associative array where the keys represent the terms and the values represent the definitions. By using nested loops, you can loop through each key-value pair and display them in a table format with two columns: one for the terms and one for the definitions.
<?php
$glossary = array(
"PHP" => "Hypertext Preprocessor",
"HTML" => "Hypertext Markup Language",
"CSS" => "Cascading Style Sheets"
);
echo "<table border='1'>";
echo "<tr><th>Term</th><th>Definition</th></tr>";
foreach ($glossary as $term => $definition) {
echo "<tr><td>$term</td><td>$definition</td></tr>";
}
echo "</table>";
?>
Keywords
Related Questions
- What is the issue with checking for the existence of $newItem in the $_SESSION['items'] array?
- What are the potential reasons for image dropouts when using PHP scripts to fetch camera snapshots for home automation?
- Is it possible to use an API instead of directly scraping content from a webpage in PHP to achieve the desired functionality?