How can one display the corresponding images for each vehicle in a PHP script similar to websites like mobile.de?
To display corresponding images for each vehicle in a PHP script similar to websites like mobile.de, you can store the image file paths in a database along with the vehicle information. Then, retrieve the image file paths based on the vehicle ID and display them on the webpage using HTML and PHP.
<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "vehicles";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve vehicle information from database
$vehicle_id = $_GET['vehicle_id'];
$sql = "SELECT * FROM vehicles WHERE id = $vehicle_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<h1>" . $row['make'] . " " . $row['model'] . "</h1>";
echo "<p>Price: $" . $row['price'] . "</p>";
// Retrieve and display corresponding images
$sql_images = "SELECT * FROM images WHERE vehicle_id = $vehicle_id";
$result_images = $conn->query($sql_images);
if ($result_images->num_rows > 0) {
while($row_image = $result_images->fetch_assoc()) {
echo "<img src='" . $row_image['image_path'] . "' alt='Vehicle Image'>";
}
} else {
echo "No images available for this vehicle.";
}
}
} else {
echo "Vehicle not found.";
}
$conn->close();
?>