gpt4 book ai didi

php - 有没有办法从数据库返回用户测试中未正确完成的问题和答案?

转载 作者:行者123 更新时间:2023-11-29 16:09:35 27 4
gpt4 key购买 nike

我正在写一个简短的 php 测验测验。对于未正确完成的问题,我希望代码能够返回问题编号和假定答案,对于正确的问题我希望增加分数

到目前为止,编码正在工作,但是当我尝试将错误问题的数量及其正确答案添加到数组 $wrong_answer 时,我得到了堆栈,并且无法找出解决方法

<?php include 'database.php'; ?>
<?php session_start(); ?>
<?php

//Check to see if score is set_error_handler
if (!isset($_SESSION['score'])){
$_SESSION['score'] = 0;
}


//Check if form was submitted
if($_POST){

$number = $_POST['number'];
$selected_choice = $_POST['choice'];
$next=$number+1;
$wrong_answer = array();
$lesson = (int) $_GET['l'];

//Get total number of questions
$query = "select * from questions where lesson_number = $lesson";
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total=$results->num_rows;

//Get correct choice
$q = "select * from `choices` where question_number = $number and is_correct=1";
$result = $mysqli->query($q) or die($mysqli->error.__LINE__);
$row = $result->fetch_assoc();
$correct_choice=$row['id'];



//compare answer with result
if($correct_choice == $selected_choice){
$_SESSION['score']++;
}else {
foreach($number and $correct_choice){
$wrong_answer [] = $number, $correct_choice;
}
}

if($number == $total){
header("Location: final.php");
exit();
} else {
header("Location: B.php?n=".$next."&l=$lesson&score=".$_SESSION['score']);
}
}
?>

最佳答案

当变量不是数组时,为什么要编写 foreach 循环?你想做什么吗

if($correct_choice == $selected_choice){
$_SESSION['score']++;
}else {
$wrong_answer=[$number,$correct_choice];
}

if($correct_choice == $selected_choice){
$_SESSION['score']++;
}else {
array_push($wrong_answer,[$number, $correct_choice]);//for 2d array
}

关于php - 有没有办法从数据库返回用户测试中未正确完成的问题和答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55384722/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com