基于回答的问题总结点数

时间:2013-04-24 18:39:41

标签: php sum match

该代码是针对单一课程的简答题测验。我想做的是匹配学生对存储在数据库中的答案的回应。匹配的答案(或关键字)计为一个点。我在计算总积分时遇到了麻烦。 preg_match()结果已经正确。这是代码:

    <?php
    ini_set('display_errors',1);
    error_reporting(E_ALL ^ E_NOTICE);
    include("dbconn.php");
    session_start();

    if(isset($_POST['Submit']))
    {   
    $id = $_SESSION['tf1_sid'];
    $qno = $_POST['q_no'];
    ?>

    <head></head>
    <body>
    <form id="form1" name="form1" method="post" action="">
    <table width="590" border="1" cellpadding="2" align="center">
    <?php

    //db query to obtain i_id - to insert to RESULT table
    $sql_i = "SELECT i_id FROM ins_stud WHERE s_id = '$id'";
    $query_i = mysql_query($sql_i) or die("MySQL Error: " . mysql_error());
    $data_i = mysql_fetch_assoc($query_i);
    $ins_id = $data_i['i_id'];
    //echo $ins_id;

    //$correct = 0;
    $total = 0;
    $arr_ind = 1;
    $atext = array(1);
    $ans = array(1);

    for($i=1;$i<=$qno;$i++){
    $repStr = str_replace("1", $i, "answer_1");
    //echo "Question ". $i .": ". $repStr;
    $ans[] = $_POST[$repStr];
    //echo $ans;

    $sql_check = "SELECT q_ans FROM question WHERE q_id='$i'";
    $query_ch = mysql_query($sql_check) or die("MySQL Error: " . mysql_error());
    $data_ch = mysql_fetch_assoc($query_ch);
    $atext[] = $data_ch['q_ans'];

    // insert answer to table
    //$sql_eval = "INSERT INTO eval_set (s_id, q_id, response, response_value, created) VALUES ('" . $id . "', '" . $i . "', '" . $ans . "', '" . $correct . "', CURDATE())";
    //mysql_query($sql_eval) or die ("Error: " . mysql_error());        
    }

    // insert result to table
    //$sql_result = "INSERT INTO result (r_score, s_id, i_id) VALUES ('" . $total . "','" . $id . "','" . $ins_id . "')";
    //mysql_query($sql_result) or die ("Error: " . mysql_error());

    // db query for questions
    $sql_q  = "SELECT q_id, q_no, q_text, q_ans, q_help FROM question";
    $query_q = mysql_query($sql_q) or die("MySQL Error: " . mysql_error());

    // start loop for questions & answers
    $rad = 1;
    while($data_q = mysql_fetch_array($query_q, MYSQL_ASSOC)){     

    echo "<tr><td width='20' align='center' valign='top'><label><br><input name='q_no' size='1' type='hidden' value=". $data_q['q_no'] .">". $data_q['q_no'] ."</label></td>";
    echo "<td><p align='justify'>". $data_q['q_text'] ."<br />";

    if(preg_match_all("/". $ans[$arr_ind]. "/i", " . $atext[$arr_ind] . ")){
    echo "Something matches";
$total = total + 1;
}
    //else if (preg_match("/^$/", " . $atext[$arr_ind] . "))
    //echo "Empty string";
    else
    echo "Wrong";

    echo "<p align='justify'><b>YOUR ANSWER: </b>". $ans[$arr_ind]. "</p>";
    echo "<p align='justify'><label><b>SUGGESTED ANSWER:</b> <br><input name='answer_".$rad."' type='hidden' value=''>". $atext[$arr_ind] . "</label></p>";

    $rad++;
    $arr_ind++;
    }
    mysql_free_result($query_q);
    include("dbconn.php");

    echo "</table>";
    echo "<h2>" . $total . " questions correct. - Answer Review</h2>";  
    echo "</form>";
    ?>
    </body>
    </html>
    <?php
    }
    else
    {
    header("Location:s_login.php");
    }
    // close db connection
    mysql_close($dbconn);
    ?>

1 个答案:

答案 0 :(得分:1)

你错过了$ in:

$total = total + 1;

或者只是使用:

$total++;