How can PHP developers effectively sort and manipulate data from text files in HTML tables for display purposes?
To effectively sort and manipulate data from text files in HTML tables for display purposes, PHP developers can read the text file line by line, extract the necessary data, store it in an array, sort the array based on specific criteria, and then output the sorted data in an HTML table format.
<?php
// Read the text file line by line
$lines = file('data.txt');
// Initialize an empty array to store the extracted data
$data = [];
// Extract the necessary data from each line and store it in the array
foreach ($lines as $line) {
$parts = explode(',', $line);
$data[] = [
'name' => $parts[0],
'age' => $parts[1],
'email' => $parts[2]
];
}
// Sort the data array based on the 'name' field
usort($data, function($a, $b) {
return $a['name'] <=> $b['name'];
});
// Output the sorted data in an HTML table format
echo '<table>';
echo '<tr><th>Name</th><th>Age</th><th>Email</th></tr>';
foreach ($data as $row) {
echo '<tr><td>' . $row['name'] . '</td><td>' . $row['age'] . '</td><td>' . $row['email'] . '</td></tr>';
}
echo '</table>';
?>
Related questions
-
SQL injection prepared statements PDO
-
SQL injection XSS vulnerability code maintenance
-
validation sanitization prepared statements