[Asm] 纯文本查看 复制代码 <?php
if(php_sapi_name()!="cli"){
require $_SERVER["DOCUMENT_ROOT"].'/pdo/eh/a17/visit_log/log_visit.php';
?>
<?php
// Step 1: Connect to the SQLite database
$db = new PDO('sqlite:'.$_SERVER["DOCUMENT_ROOT"].'/db');
// Step 2: Retrieve data from the database table
$query = "SELECT question, correct_answer, option_a, option_b, option_c, option_d FROM questionnaire";
$statement = $db->query($query);
$questions = $statement->fetchAll(PDO::FETCH_ASSOC);
// Step 3: Generate the HTML form dynamically
echo '<form action="process_form.php" method="POST">';
foreach ($questions as $question) {
echo '<p>' . $question['question'] . '</p>';
$correctAnswers = explode(',', $question['correct_answer']);
if(empty($question['option_a'])){
echo '<textarea name="' . $question['question'] . '" ></textarea><br>';
continue;
}
if (count($correctAnswers) > 1) {
// Multiple correct answers, use checkboxes
echo '<input type="checkbox" name="' . $question['question'] . '[]" value="A">' . $question['option_a'] . '<br>';
echo '<input type="checkbox" name="' . $question['question'] . '[]" value="B">' . $question['option_b'] . '<br>';
echo '<input type="checkbox" name="' . $question['question'] . '[]" value="C">' . $question['option_c'] . '<br>';
echo '<input type="checkbox" name="' . $question['question'] . '[]" value="D">' . $question['option_d'] . '<br><br>';
} else {
// Single correct answer, use radio buttons
for($i=0;$i<4;$i++){
if(!empty($question['option_'.chr(ord('a')+$i)] )){
echo '<input type="radio" name="' . $question['question'] . '" value="'.chr(ord("A")+$i).'">' . $question['option_'.chr(ord('a')+$i)] . '<br>';
}
}
/*
echo '<input type="radio" name="' . $question['question'] . '" value="A">' . $question['option_a'] . '<br>';
echo '<input type="radio" name="' . $question['question'] . '" value="B">' . $question['option_b'] . '<br>';
echo '<input type="radio" name="' . $question['question'] . '" value="C">' . $question['option_c'] . '<br>';
echo '<input type="radio" name="' . $question['question'] . '" value="D">' . $question['option_d'] . '<br><br>';
*/
}
}
echo '<input type="submit" value="Submit">';
echo '</form>';
// Step 4: Process form submission in "process_form.php" file
// You need to handle the form submission and check the answers against the correct answers stored in the database.
?>
|