2012-07-10 89 views
0

我从MySQL动态生成一组单选按钮。按钮正在创建,分配给它们的变量正在填充,因为我做了一个echo print_r并显示了该变量的数组。我现在想比较由此产生的值,如果价值是“0”,我想插入一个分数,并呈现一个绿色的检查图形和正确的字。如果值为“1”,我希望它为分数输入不同的值并呈现不正确的和红色的X图形。以下是我迄今为止(一切动态填充两个问题的答案为单选按钮):如何比较动态创建的单选按钮的值

<?php 
echo '<form id="frmQuestion" name="frmQuestion" method="post" action="QuizQuestion1.php">'; 

// Connect to the Database 
require_once('mysqli_connect.php'); 

//create the query for the question 
$q = "SELECT `Question` FROM tbl_Question WHERE QuestionID = 1"; 

//Create the query for the Answers 
$q2 = "SELECT `Answer`,`AnswerStatusID`,`AnswerResponse` FROM tbl_Answer WHERE QuestionID = 1"; 

//Run the query 
$r = mysqli_query($conn,$q); 

//run the answer query 
$r2 = mysqli_query($conn,$q2); 

while($row = mysqli_fetch_array($r,MYSQLI_ASSOC)){ 
    echo '<div id="Question1"><p> ' . $row['Question'] . '</div></p>'; 
} 

//Declare the variables as a array 
$AnswerResponse = array(); 
$AnswerStatusID = array(); 

while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){ 
    echo '<div id="Question1"><input name="q1" type="radio" value="'.$AnswerStatusID.'"/>' . $row2['Answer'] . '</div><br/>'; 

    //Assign the AnswerStatusID to a var 
    $AnswerStatusID[] = $row2['AnswerStatusID']; 

    //Assign the AnswerResponse to a var 
    $AnswerResponse[] = $row2['AnswerResponse']; 
} 

//Create the submit button 
echo '<input type="submit" value="Submit Answer" name="submit"/>'; 
echo '</form>'; 

//Logic for correct or incorrect answers 
if (isset($_POST['q1']) && ($_POST['q1'] == '0')) 
{ 
    //create the query for the score 
    $q3 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('100%','1')"; 

    //Run the query 
    $r = @mysqli_query ($conn,$q3); 

    if($r){ 

     //Confirm message data was entered with a correct response and a graphic 
     echo '<h1>Correct!!</h1><img src="/images/green_Check_Low.jpg" alt="Green Check"/>'; 
     echo '<a href="QuizQuestion2.php">Click here for the next question</a>'; 
    } 
    else 
    { 
     //there was an error 
     echo'<h1>System error</h1>'; 

     //Debugging message 
     echo'<p>' . mysqli_error($conn) . '<br/><br/>Query:' . $q3 . '</p>'; 

    }//End of nested IF 
} 
else{ 
    //create the query for the score 
    $q4 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('0%','1')"; 

    //Run the query 
    $r2 = @mysqli_query ($conn,$q3); 

    if($r2){ 

     //Confirm message data was entered with a correct response and a graphic 
     echo '<h1>Incorrect!!</h1><img src="/images/red_X_Low.jpg" alt="Red X"/>'; 
     echo '<a href="QuizQuestion2.php">Click here for the next question</a>'; 
    } 
    else 
    { 
     //there was an error 
     echo'<h1>System error</h1>'; 

     //Debugging message 
     echo'<p>' . mysqli_error($conn) . '<br/><br/>Query:' . $q3 . '</p>'; 

    }//End of nested IF 

} 

//Free up the results for the Question query 
mysqli_free_result($r); 

//Free up the results from the Answer query 
mysqli_free_result($r2); 

//close the DB connection 
mysqli_close($conn); 

?> 
+1

不发布完整的代码。只发布相关的代码。顺便说一句,在给定的代码中没有复选框 – diEcho 2012-07-10 17:58:05

+0

有一个单选按钮' ametren 2012-07-10 17:59:08

+0

是的,它是一个单选按钮,并且所有发布的代码都相关,因为您需要查看按钮的填充位置和创建位置,以便我能够看到它是什么我在问 – 2012-07-10 18:01:53

回答

0

这就是答案和工作按预期。感谢每个人的输入。

//Declare the variables as a array 
$AnswerResponse = array(); 
$AnswerStatusID = array(); 

while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){ 
echo '<div id="Question1"><input name="q1" type="radio" value="'.$row2['AnswerStatusID'].'"/>' . $row2['Answer'] . '</div><br/>'; 

//Assign the AnswerStatusID to a var 
$AnswerStatusID[] = $row2['AnswerStatusID']; 

//Assign the AnswerResponse to a var 
$AnswerResponse[] = $row2['AnswerResponse']; 
} 




//Create the submit button 
echo '<input type="submit" value="Submit Answer" name="submit"/>'; 
echo '<input type="hidden"name="submitted"value="TRUE"/>'; 
echo '</form>'; 



if($_POST['submitted']) { 

//Logic for correct or incorrect answers 
if (isset($_POST['q1']) && ($_POST['q1'] == '0')) 
{ 
//create the query for the score 
$q3 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('100%','1')"; 

//Run the query 
$r3 = @mysqli_query ($conn,$q3); 

//Confirm message data was entered with a correct response and a graphic 
echo '<h1>Correct!!</h1><img src="/images/green_Check_Low.jpg" alt="Green Check"/>'; 
echo '<a href="QuizQuestion2.php">Click here for the next question</a>'; 


}  
else{ 
//create the query for the score 
$q4 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('0%','1')"; 

//Run the query 
$r4 = @mysqli_query ($conn,$q4); 

//Confirm message data was entered with a correct response and a graphic 
echo '<h1>Incorrect!!</h1><img src="/images/red_X_Low.jpg" alt="Red X"/><br/>'; 
echo '<a href="QuizQuestion1.php">Click here to try again</a>'; 

} 

}