2017-01-05 10 views
-1

私はオンライン試験システムを構築しようとしています。私はデータベースから質問を取り出して一つずつ表示したいのです。つまり、ユーザーは次の質問を見るために「次へ」をクリックします。私はJQueryを使いたい。何か案は?PHPオンライン試験

は、ここに私のPHPコードデータベースは20の以上の質問があり

$getTests = $conn->prepare("SELECT * FROM grammar_test WHERE active = ? ORDER BY RAND() LIMIT 20"); 
$getTests->execute(array(1)); 
$rowTests = $getTests->fetch(\PDO::FETCH_ASSOC);` 

ですが、私は唯一の20のランダムな質問をしたいです。ここで

はHTMLである(2つのフィールドセットにTrancatedが、彼らは実際には20です)

<form action="" class="grammar-test-form " method="POST" accept-charset="utf-8"> 

<fieldset id="first"> 
    <?php if(!empty($rowTests['instructions'])){ echo '<h4>1. '.$rowTests['instructions'].'</h4>';}?> 
    <?php if(!empty($rowTests['question'])){ echo '<h4>'.$rowTests['question'].'</h4>';}?> 
    <?php if(!empty($rowTests['instructions'])){ echo '<input type="radio" name="'.$rowTests['question_id'].'"> '.$rowTests['choice_1'].'<br/>';}?> 
    <?php if(!empty($rowTests['instructions'])){ echo '<input type="radio" name="'.$rowTests['question_id'].'"> '.$rowTests['choice_2'].'<br/>';}?> 
    <?php if(!empty($rowTests['instructions'])){ echo '<input type="radio" name="'.$rowTests['question_id'].'"> '.$rowTests['choice_3'].'<br/>';}?> 
    <?php if(!empty($rowTests['instructions'])){ echo '<input type="radio" name="'.$rowTests['question_id'].'"> '.$rowTests['choice_4'].'<br/>';}?> 
    <hr/> 
    <button class="next_btn btn btn-3d btn-green" name="next" type="button">Next &raquo;</button> 
</fieldset> 

<fieldset> 
    <?php if(!empty($rowTests['instructions'])){ echo '<h4>2. '.$rowTests['instructions'].'</h4>';}?> 
    <?php if(!empty($rowTests['question'])){ echo '<h4>'.$rowTests['question'].'</h4>';}?> 
    <?php if(!empty($rowTests['instructions'])){ echo '<input type="radio" name="'.$rowTests['question_id'].'"> '.$rowTests['choice_1'].'<br/>';}?> 
    <?php if(!empty($rowTests['instructions'])){ echo '<input type="radio" name="'.$rowTests['question_id'].'"> '.$rowTests['choice_2'].'<br/>';}?> 
    <?php if(!empty($rowTests['instructions'])){ echo '<input type="radio" name="'.$rowTests['question_id'].'"> '.$rowTests['choice_3'].'<br/>';}?> 
    <?php if(!empty($rowTests['instructions'])){ echo '<input type="radio" name="'.$rowTests['question_id'].'"> '.$rowTests['choice_4'].'<br/>';}?> 
    <hr/> 
    <button class="pre_btn btn btn-3d btn-green" name="previous" type="button">Previous</button> 
    <button class="next_btn btn btn-3d btn-green" name="next" type="button">Next &raquo;</button> 
</fieldset> 
</form> 

はここで別の

$(document).ready(function() { 
     $(".next_btn").click(function() { 

     $(this).parent().next().fadeIn('slow'); 
     $(this).parent().css({ 
     'display': 'none' 
     }); 


     $('.active').next().addClass('active'); 
     }); 
     $(".pre_btn").click(function() { 
     $(this).parent().prev().fadeIn('slow'); 
     $(this).parent().css({ 
     'display': 'none' 
     }); 


     $('.active:last').removeClass('active'); 
     }); 
    }); 

すべて1つのフィールドセットから移動することはJQueryで正常に動作します。問題は1つの質問が選択され、20回繰り返されることです。私は20種類の質問をしたい。何か案が? whileループのアイデアがありますが、最初と最後のフィールドセットで問題が発生します。最初のボタンは1つのみ(次へ)、次の18は2つのボタン(次と前)、最後のフィールド2つのボタン(PreviousとSubmit)

+0

"問題は1つの質問が選択され、20回繰り返されます":SQLクエリで20種類の質問があるかどうかを確認します。私はループと何かが間違っていると思う、多分同じ質問IDを取る、foreachを試してみてください。より正確な答えを得るためのコードをもっと与えることができれば、どうぞ。 –

+0

タイトルに固有の問題を記述してください。 –

+0

_ "すべてうまくいきます。問題は" _うまく動作しません。 :) –

答えて

1

あなたのコードは、私が思うような書式で少し混乱しています。私が見ることができるものから、一度だけフェッチを呼び出す(データの最初の行を取る)。 ループ内でフェッチを行い、次の質問を取得して、それをあなたのhtmlやあなたのものに組み立てたいと思うでしょう。

例2は、ここでは良いパターンを示すようです。あなたの中にフェッチください: PDOStatement::fetch

私はそのリンクから意味一部:

while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) { 
    $data = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\n"; 
    print $data; 
} 

は、より多くの例を追加しました...これは、各行(質問)を取得するためにループを使用して、質問を出力示し。これは、同じコードのほぼ同じコピーを20個持つ代わりに使用します。

<?php 
while ($rowTests = $getTests->fetch(\PDO::FETCH_ASSOC)) { 
?> 
    <fieldset> 
     <?php if(!empty($rowTests['instructions'])){ echo '<h4>1. '.$rowTests['instructions'].'</h4>';}?> 
     <?php if(!empty($rowTests['question'])){ echo '<h4>'.$rowTests['question'].'</h4>';}?> 
     <?php if(!empty($rowTests['instructions'])){ echo '<input type="radio" name="'.$rowTests['question_id'].'"> '.$rowTests['choice_1'].'<br/>';}?> 
     <?php if(!empty($rowTests['instructions'])){ echo '<input type="radio" name="'.$rowTests['question_id'].'"> '.$rowTests['choice_2'].'<br/>';}?> 
     <?php if(!empty($rowTests['instructions'])){ echo '<input type="radio" name="'.$rowTests['question_id'].'"> '.$rowTests['choice_3'].'<br/>';}?> 
     <?php if(!empty($rowTests['instructions'])){ echo '<input type="radio" name="'.$rowTests['question_id'].'"> '.$rowTests['choice_4'].'<br/>';}?> 
     <hr/> 
     <button class="next_btn btn btn-3d btn-green" name="next" type="button">Next &raquo;</button> 
    </fieldset> 
<?php 
}//end of while loop 
?> 

わかりやすくするために、最初、中期、最後の質問のボタンに関するロジックは含まれていませんでした。

異なるボタンを持つ最初と最後の質問に対処するには、質問が最初か最後かを条件付きで知ることができます(#1または#20)か、適切なボタンを表示または非表示にする必要があります。あなたはjQueryでこれを行うこともできますし、PHPでボタンを作成させることもできません。もう一度、あなたの選択。

+0

この回答は興味深く有益です。ありがとう。しかし、私はそれを得ていません。少し明瞭にするか、例を挙げてください。ところで、私はPDO :: FETCH_ORI_NEXTに出くわしていませんでした。新しいことが学びました。大いに感謝します – Neville

+0

@Neville私はあなたの状況のた​​めにループについて私が何を意味するかをより良く示すために別の例を追加しました。 ** while **は質問を囲んでいるので、質問を一度コード化し、フェッチ操作で取得したデータで埋めておくだけで済みます。 –