How can PHP functions be utilized to streamline the process of filtering and displaying specific data in a table?
To streamline the process of filtering and displaying specific data in a table using PHP functions, you can create a function that takes in the data to be displayed and a filter criteria. This function can then iterate through the data, apply the filter, and output the filtered data in a table format.
<?php
// Function to filter and display data in a table
function displayFilteredData($data, $filter) {
echo "<table>";
foreach ($data as $row) {
if ($row['criteria'] == $filter) {
echo "<tr>";
echo "<td>" . $row['column1'] . "</td>";
echo "<td>" . $row['column2'] . "</td>";
// Add more columns as needed
echo "</tr>";
}
}
echo "</table>";
}
// Example data
$data = array(
array('criteria' => 'filter1', 'column1' => 'Data1', 'column2' => 'Data2'),
array('criteria' => 'filter2', 'column1' => 'Data3', 'column2' => 'Data4'),
// Add more rows as needed
);
// Call the function with data and filter criteria
displayFilteredData($data, 'filter1');
?>