How can PHP arrays be effectively used to organize and display database query results in a structured format?
When querying a database, the results are often returned as a list of rows that need to be organized and displayed in a structured format. PHP arrays can be effectively used to store and manipulate these results. By storing each row of data as an array within a larger array, you can easily loop through the results and display them in a structured format on a webpage.
// Assume $results is the array containing the database query results
// Initialize an empty array to store the structured data
$structuredData = [];
// Loop through each row of the query results
foreach ($results as $row) {
// Store each row as an array within the structured data array
$structuredData[] = $row;
}
// Display the structured data in a formatted way
foreach ($structuredData as $data) {
echo "Name: " . $data['name'] . "<br>";
echo "Age: " . $data['age'] . "<br>";
echo "Email: " . $data['email'] . "<br><br>";
}
Keywords
Related Questions
- How can the structure of the HTML form in the provided code affect the data passed to the PHP script and its processing?
- How can PHP be used to iterate through all tables in a database and count the entries in each table?
- What is the best practice for incrementing a numerical value in PHP without inadvertently treating it as a string?