How can PHP be utilized to create a FAQ page with links to specific questions?
To create a FAQ page with links to specific questions using PHP, you can utilize an array to store the questions and answers, then loop through the array to display each question as a link that when clicked, expands to show the answer. You can use JavaScript to toggle the visibility of the answer when the link is clicked.
<?php
// FAQ array containing questions and answers
$faq = [
"Question 1" => "Answer 1",
"Question 2" => "Answer 2",
"Question 3" => "Answer 3"
];
// Loop through the FAQ array to display questions as links
foreach ($faq as $question => $answer) {
echo "<a href='#' onclick='toggleAnswer(\"$question\")'>$question</a><br>";
echo "<div id='$question' style='display: none;'>$answer</div><br>";
}
?>
<script>
// JavaScript function to toggle answer visibility
function toggleAnswer(question) {
var answer = document.getElementById(question);
if (answer.style.display === "none") {
answer.style.display = "block";
} else {
answer.style.display = "none";
}
}
</script>
Keywords
Related Questions
- What is the best approach to implementing different session lifetimes for various types of sessions in PHP without creating separate directories for each type?
- What are the potential benefits of using UTF-8 encoding for handling different languages in PHP?
- What are potential pitfalls when allowing users to choose different worlds in a PHP application?