How can you create a table in PHP to display names and entry dates, with the ability to group names by date?
To display names and entry dates in a table grouped by date in PHP, you can achieve this by first sorting the data by date and then looping through the sorted data to display each name under the corresponding date. You can use an associative array to group names by date and then iterate over the array to output the table.
<?php
// Sample data array with names and entry dates
$data = array(
array('name' => 'Alice', 'date' => '2022-01-01'),
array('name' => 'Bob', 'date' => '2022-01-01'),
array('name' => 'Charlie', 'date' => '2022-01-02'),
array('name' => 'David', 'date' => '2022-01-02'),
);
// Group names by date
$groupedData = array();
foreach ($data as $entry) {
$groupedData[$entry['date']][] = $entry['name'];
}
// Output the table
echo '<table>';
foreach ($groupedData as $date => $names) {
echo '<tr><th colspan="2">' . $date . '</th></tr>';
foreach ($names as $name) {
echo '<tr><td>' . $name . '</td></tr>';
}
}
echo '</table>';
?>
Keywords
Related Questions
- What potential pitfalls should be considered when modifying PHP scripts for time selection on a website?
- What are the potential pitfalls of using the mysql_ functions in PHP and why should developers consider switching to mysqli_ or PDO?
- What are the potential risks of not properly sanitizing or escaping data before displaying it on a webpage in PHP?