How can PHP be used to split text from a database column and display it in a specific format in an HTML table?
To split text from a database column and display it in a specific format in an HTML table, you can use the explode() function in PHP to separate the text into an array based on a delimiter. Then, you can iterate over the array to format the data as needed and display it in an HTML table.
<?php
// Assume $data is the text retrieved from the database column
$data = "John,Doe,30|Jane,Smith,25|Mike,Johnson,35";
// Split the text into an array based on the delimiter '|'
$rows = explode("|", $data);
// Display the data in an HTML table
echo "<table>";
echo "<tr><th>First Name</th><th>Last Name</th><th>Age</th></tr>";
foreach ($rows as $row) {
$columns = explode(",", $row);
echo "<tr>";
foreach ($columns as $column) {
echo "<td>$column</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Keywords
Related Questions
- What is the difference between using htmlspecialchars() and htmlentities() functions in PHP for converting special characters, and which one is recommended for this scenario?
- How can the use of associative arrays improve the readability and efficiency of PHP code?
- What are common pitfalls to avoid when working with date formats in PHP?