2016-05-06 4 views
1

私は複数の選択肢を作りたいと思っており、質問はデータベースから来ています。私は自分のデータベース名が正しいと確信していますが、次のをクリックすると、質問は変更されず、そのまま終了/質問の結果になり、質問もランダムではありません。私がデータベースに挿入した最後の質問がWebページに表示されます。データベースからのデータを複数選択する

<?php 
require_once('includes/db_conn.php'); 
$query = "select * from question"; 
$query_result = $dbc->query($query); 
$num_questions_returned = $query_result->num_rows; 

if ($num_questions_returned < 1){ 
    echo "There is no question in the database"; 
    exit();} 
$questionsArray = array(); 
while ($row = $query_result->fetch_assoc()){ 
    $questionsArray[] = $row; 
} 

$correctAnswerArray = array(); 
foreach($questionsArray as $question){ 
    $correctAnswerArray[$question['question']] = $question['correct_answer']; 
} 

$questions = array(); 
foreach($questionsArray as $question) { 
    $questions[$question['question']] = $question['question']; 
} 

$choices = array(); 
foreach ($questionsArray as $row) { 
    $choices[$row['question']] = array($row['wrong_answer1'], $row['wrong_answer2'], $row['wrong_answer3'], $row['correct_answer']); 
    } 

error_reporting(0); 
$address = ""; 
$randomizequestions ="yes"; 

$a = array(
1 => array(
    0 => $question['question'], 
    1 => $row['wrong_answer1'], 
    2 => $row['wrong_answer2'], 
    3 => $row['wrong_answer3'], 
    4 => $row['correct_answer'], 
    6 => 4 
), 
); 

$max=1; 

$question=$_POST["question"] ; 

if ($_POST["Randon"]==0){ 
     if($randomizequestions =="yes"){$randval = mt_rand(1,$max);}else{$randval=1;} 
     $randval2 = $randval; 
     }else{ 
     $randval=$_POST["Randon"]; 
     $randval2=$_POST["Randon"] + $question; 
       if ($randval2>$max){ 
       $randval2=$randval2-$max; 
       } 
     } 

$ok=$_POST["ok"] ; 

if ($question==0){ 
     $question=0; 
     $ok=0; 
     $percentage=0; 
     }else{ 
     $percentage= Round(100*$ok/$question); 
     } 
?> 

<HTML><HEAD> 

<SCRIPT LANGUAGE='JavaScript'> 
<!-- 
function Goahead (number){ 
     if (document.percentaje.response.value==0){ 
       if (number==<?php print $a[$randval2][6] ; ?>){ 
         document.percentaje.response.value=1 
         document.percentaje.question.value++ 
         document.percentaje.ok.value++ 
       }else{ 
         document.percentaje.response.value=1 
         document.percentaje.question.value++ 
       } 
     } 
     if (number==<?php print $a[$randval2][6] ; ?>){ 
       document.question.response.value="Correct" 
     }else{ 
       document.question.response.value="Incorrect" 
     } 
} 
// --> 
</SCRIPT> 

</HEAD> 
<BODY BGCOLOR=FFFFFF> 

<CENTER> 
<H1><?php print "$title"; ?></H1> 
<TABLE BORDER=0 CELLSPACING=5 WIDTH=500> 

<?php if ($question<$max){ ?> 

<TR><TD ALIGN=RIGHT> 
<FORM METHOD=POST NAME="percentaje" ACTION="<?php print $URL; ?>"> 

<BR>Percentaje of correct responses: <?php print $percentage; ?> % 
<BR><input type=submit value="Next >>"> 
<input type=hidden name=response value=0> 
<input type=hidden name=question value=<?php print $question; ?>> 
<input type=hidden name=ok value=<?php print $ok; ?>> 
<input type=hidden name=Randon value=<?php print $randval; ?>> 
<br><?php print $question+1; ?>/<?php print $max; ?> 
</FORM> 
<HR> 
</TD></TR> 

<TR><TD> 
<FORM METHOD=POST NAME="question" ACTION=""> 
<?php print "<b>".$a[$randval2][0]."</b>"; ?> 

<BR>  <INPUT TYPE=radio NAME="option" VALUE="1" onClick=" Goahead (1);"><?php print $a[$randval2][1] ; ?> 
<BR>  <INPUT TYPE=radio NAME="option" VALUE="2" onClick=" Goahead (2);"><?php print $a[$randval2][2] ; ?> 
<?php if ($a[$randval2][3]!=""){ ?> 
<BR>  <INPUT TYPE=radio NAME="option" VALUE="3" onClick=" Goahead (3);"><?php print $a[$randval2][3] ; } ?> 
<?php if ($a[$randval2][4]!=""){ ?> 
<BR>  <INPUT TYPE=radio NAME="option" VALUE="4" onClick=" Goahead (4);"><?php print $a[$randval2][4] ; } ?> 

<BR>  <input type=text name=response size=8> 


</FORM> 

<?php 
}else{ 
?> 
<TR><TD ALIGN=Center> 
The Quiz has finished 
<BR>Percentage of correct responses: <?php print $percentage ; ?> % 
<p><A HREF="<?php print $address; ?>">Home Page</a> 

<?php } ?> 

</TD></TR> 
</TABLE> 

</CENTER> 
</BODY> 
</HTML> 

これが私のプロセスでは、データベースにデータを追加します:あなたは$の選択肢を使用していない理由を

<?php 
include('includes/header.html'); 

error_reporting(-1); 
ini_set('display_errors', 'On'); 

//Check for empty fields 
if(empty($_POST['question'])|| 
    empty($_POST['correct_answer']) || 
    empty($_POST['wrong_answer1'])  || 
    empty($_POST['wrong_answer2'])  || 
    empty($_POST['wrong_answer3'])) 
{ 
    echo "Please complete all fields"; 
    exit(); 
} 

//Create short variables 
$question = $_POST['question']; 
$correct_answer = ($_POST['correct_answer']); 
$wrong_answer1 = ($_POST['wrong_answer1']); 
$wrong_answer2 = ($_POST['wrong_answer2']); 
$wrong_answer3 = ($_POST['wrong_answer3']); 

//connect to the database 
require_once('includes/db_conn.php'); 

//Create the insert query 
$query = "INSERT INTO question VALUES ('$question', '$correct_answer', '$wrong_answer1','$wrong_answer2','$wrong_answer3')"; 

$result = $dbc->query($query); 

if($result){ 
    echo "Your quiz has been saved"; 
} else { 
    echo '<h1>System Error</h1>'; 
} 
$dbc->close(); 

?> 

答えて

0

あなたの最初の間違いは、$の列を作っている、これは私のコードである

を助けてくださいあなたが最後にループのために作ったものと無作為な質問のために$ choices配列を処理し、次のsubmitボタンがフォームの外にあるので、次のボタンでフォーム値を取得しません。

ラジオボタンをクリックすると何も書き込まれません。クリックイベントの送信時に関数を記述し、前の回答を保存した後に次の質問を処理します。

これは私の提案です。ここでは、データベースのバックアップとその接続ファイルを追加して、コーディングを手伝ってもらうのに役立ちます。

関連する問題