2012-07-10 94 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>'; 

} 

} 
相關問題