0

Hi. I'm creating a short-answer quiz. The questions has one text area each for answer. However, for each question there are several answer suggestions in the database. I'd like to compare the value post by the text area with the suggestions.

            $answers = explode(" ", $ans[$arr_ind]); //$ans[$arr_ind] holds answers from form
            foreach ($answers as $answer) {
                $answer = trim($answer);

                if(stripos($atext[$arr_ind], $answer)!== false){ //$atext[$arr_ind] holds answer suggestions
                    //correct
                    $result = "<p align='justify'><img src='image/mark.png' border='0' width='20' height='20'></img><b> YOUR ANSWER: </b>". $ans[$arr_ind]. "</p>";
                    //$correct = 1;
                }
                else
                $result = "<p align='justify'><img src='image/cross.png' border='0' width='20' height='20'></img> <b>YOUR ANSWER: </b>". $ans[$arr_ind]. "</p>";
            }//foreach  

            echo $result;
            ///////////////// COMPARE USER INPUT W/ ANSWERS /////////////////

Right now the code only compares with the first suggestion it finds. Any help would be very much appreciated. Thanks.

Atikah

2
Contributors
2
Replies
13
Views
4 Years
Discussion Span
Last Post by atikah8890
0

you seem to be checking the string position in an array?

if(stripos($atext[$arr_ind], ...

or is this part of a loop that you haven't told us about?

Edited by diafol

0

It's part of a while loop for the questions. Here's the full code:

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

if(isset($_POST['Submit']))
{   
$id = $_SESSION['tf1_sid'];
$qno = $_POST['q_no'];
?>
<head>
<title>Dahlia | Formative Assessment</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<h2>Answer Review</h2>
<table width="590" border="0" 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'];

$arr_ind = 1;
$atext = array(1);
$ans = array(1);
$ans_stat = false;

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

    //to compare with suggestions
    $sql_check = "SELECT a_text FROM answer 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['a_text'];
}

    // db query for questions
    $sql_q  = "SELECT q_id, q_no, q_text, 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/>";

        ///////////////// COMPARE USER INPUT W/ ANSWERS /////////////////
        $answers = explode(" ", $ans[$arr_ind]);
        foreach ($answers as $answer) {
            $answer = trim($answer);

            if(stripos($atext[$arr_ind], $answer)!== false){
                //correct
                $ans_stat = true;
                $result = "<p align='justify'><img src='image/mark.png' border='0' width='20' height='20'></img><b> YOUR ANSWER: </b>". $ans[$arr_ind]. "</p>";
                //$correct = 1;
            }
            else
            $result = "<p align='justify'><img src='image/cross.png' border='0' width='20' height='20'></img> <b>YOUR ANSWER: </b>". $ans[$arr_ind]. "</p>";
        }//foreach  

        echo $result;
        ///////////////// COMPARE USER INPUT W/ ANSWERS /////////////////

        echo "<p align='justify'><label><b><u>SUGGESTED ANSWERS</u>:</b></label></p>";

    // db query for answers
    $sql_a = "SELECT a_id, a_text FROM answer WHERE q_id=". $data_q['q_id'];
    $query_a = mysql_query($sql_a) or die("MySQL Error: " . mysql_error());

    // start loop for answers
    while($data_a = mysql_fetch_array($query_a, MYSQL_ASSOC)){
        $sel = $atext[$arr_ind];
        echo "<p align='justify'><input name='answer_".$rad."' type='hidden' value=''>". $data_a['a_text'] . "</label></p>";
        $div = "<center><img src='image/divider.png'></img></center>";
        }
    echo $div;

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

        $rad++;
        $arr_ind++;
    }

    // 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());

    mysql_free_result($query_q);
    include("dbconn.php");
    echo "</table>";
    //echo "<p align='center'><b>Score: " . $total . " points</b></p>"; 
    echo "</form>";
?>
</body>
</html>
<?php
}
else
{
header("Location:s_login.php");
}
// close db connection
mysql_close($dbconn);
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.