In what ways can PHP be utilized to display images and information from a MySQL database in a gallery format with dynamic menus?
To display images and information from a MySQL database in a gallery format with dynamic menus, you can use PHP to retrieve the data from the database and dynamically generate HTML code to display the images and information in a gallery format. You can use PHP to create dynamic menus based on the categories or tags associated with the images in the database, allowing users to filter and view specific sets of images.
<?php
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "gallery";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve images and information from database
$sql = "SELECT * FROM images";
$result = $conn->query($sql);
// Display images in a gallery format
echo '<div class="gallery">';
while($row = $result->fetch_assoc()) {
echo '<div class="image">';
echo '<img src="' . $row["image_url"] . '" alt="' . $row["image_alt"] . '">';
echo '<p>' . $row["image_description"] . '</p>';
echo '</div>';
}
echo '</div>';
// Create dynamic menu based on categories or tags
$sql = "SELECT DISTINCT category FROM images";
$result = $conn->query($sql);
echo '<div class="menu">';
echo '<ul>';
while($row = $result->fetch_assoc()) {
echo '<li><a href="?category=' . $row["category"] . '">' . $row["category"] . '</a></li>';
}
echo '</ul>';
echo '</div>';
$conn->close();
?>