How can PHP be used to display data from a MySQL database in a sortable list format?
To display data from a MySQL database in a sortable list format using PHP, you can retrieve the data from the database, store it in an array, and then use HTML and PHP to display the data in a table format. You can include headers in the table that can be clicked on to sort the data based on a specific column.
<?php
// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Retrieve data from the database
$query = "SELECT * FROM table_name";
$result = mysqli_query($connection, $query);
// Store data in an array
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
// Display data in a sortable table format
echo "<table>";
echo "<tr><th><a href=''>Column 1</a></th><th><a href=''>Column 2</a></th></tr>";
foreach ($data as $row) {
echo "<tr><td>" . $row['column1'] . "</td><td>" . $row['column2'] . "</td></tr>";
}
echo "</table>";
// Close database connection
mysqli_close($connection);
?>
Keywords
Related Questions
- Are there any alternative solutions or newer libraries available for interacting with Quake 3 servers in PHP?
- How can individual fetches from a MySQL query be addressed separately in PHP?
- What are the advantages of using a dropdown menu for selecting and downloading files in PHP instead of direct links?