In PHP, what is the recommended approach for displaying the next image after a question has been answered in a quiz format?
When displaying a quiz format where each question is followed by an image, the recommended approach for displaying the next image after a question has been answered is to store the image URLs in an array and keep track of the current question number. After the user answers a question, increment the question number and use it to access the next image URL from the array to display.
<?php
// Array of image URLs
$images = [
'image1.jpg',
'image2.jpg',
'image3.jpg',
];
// Get the current question number
$currentQuestion = isset($_GET['q']) ? $_GET['q'] : 0;
// Display the question
echo "Question " . ($currentQuestion + 1) . ": What is the answer?";
// Display the image
echo "<img src='" . $images[$currentQuestion] . "' alt='Image for question " . ($currentQuestion + 1) . "'>";
// After the question is answered, increment the question number
$newQuestion = $currentQuestion + 1;
// Display a link to the next question
echo "<a href='quiz.php?q=$newQuestion'>Next Question</a>";
?>
Keywords
Related Questions
- How can error reporting settings in PHP help in debugging issues related to inserting multiple values from a select box into a database?
- What are the potential pitfalls of relying solely on a procedural approach without utilizing a Controller class in PHP applications?
- How can you sort an array of class instances in PHP based on a specific attribute?