知識不足の初心者の質問になりますが、よろしくお願いします。
データベースに格納されている問題10件をランダムに表示させ、最後にその正答数を表示させる
という問題になります。
調べ方も悪いのかどうしても出来上がりません。そこで皆様のお力をお借りしたいです。
私の配列ではただ質問が10個羅列されるのみで、選択肢などが出ない状態です。
また、問題の下にradioボタン形式のように選択肢を表示したいのですが、
そのひも付けと表示がうまくいきません。
<イメージ図>
Q1.Which launguage is most commonly used in Japan ?
○選択肢1 ○選択肢2 ○選択肢3
<テーブル:Quiz>
<カラム:quiz_No question choice_1 choice_2 choice_3 answer >
コード <!DOCTYPE html> <?php $dsn = 'mysql:host=localhost;dbname=ex.sql'; $user = 'root'; $password = 'root'; //$questionArray = array(); try{ $db = new PDO($dsn,$user,$password); $db ->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); $sql = "SELECT * FROM quiz ORDER BY RAND()"; $stmt = $db->query($sql); $stmt->execute(); }catch(PDOException $error) { echo "error".$error->getMessage(); } foreach ($stmt as $row) { echo $row['id'].':'.$row['question'].':'.'<br><br>'.$row['choice_1'].':'.$row['choice_2'].':'.$row['choice_3']; echo '<br>'; } ?> <html> <head lang="ja"> <meta charset="UTF-8"> <title>Quiz</title> </head> <body> <input type="radio" name="choice_1[{$row ["id"]}]" value="1">{$row["choice_1"]}<br> <input type="radio" name="choice_2[{$row ["id"]}]" value="2">{$row["choice_2"]}<br> <input type="radio" name="choice_3[{$row ["id"]}]" value="3">{$row["choice_3"]}<br> </body> </html> ↓ ↓変更後////////////////////////////////////////////////////////////////////////// <!DOCTYPE html> <html> <head> <title>Sample Quiz</title> </head> <body> <?php $dsn='mysql:host=localhost;dbname=quiz'; $user='root'; $password='root'; try{ $db=new PDO($dsn,$user,$password); $db ->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); $sql="SELECT * FROM quiz ORDER BY RAND()"; $stmt=$db->query($sql); $stmt->execute(); }catch(PDOException $error){ echo "error".$error->getMessage(); } ///////////////////////////////////////////////////////////////////////////// $id='id'; $question='question'; $choice_1='choice_1'; $choice_2='choice_2'; $choice_3='choice_3'; $number = 0; $answer = 'answer'; $number = 0; for($i=0; $row = $stmt->fetch(); $i++){ $number++; $id = $row['id']; $question = $row['question']; $ans_array = array($row['choice_1'],$row['choice_2'],$row['choice_3'],$row['answer']);{ } shuffle($ans_array); //////////////////////////////////////////////////////////////////////////////////////////////Score $total = array(); foreach($answer as $key => $value): echo "question".$key."<br>"; echo "answer".$value."<be>"; echo (is_null($_POST["answer"][$key]) ? "Unanswered" : "Answer <b>" . $_POST["answer"][$key]) . "</b><br><br>"; if(!array_key_exists($value,$total)): $total[$value] = ""; endif; $total[$value] += $_POST["answer"][$key]; endforeach; foreach($total as $cat => $score): echo "<b>Answer:</b> " . $cat . " <b>Total Score:</b> " . $score . "<br>"; endforeach; ?> <h4> <?php echo $number . ".) " . $question; ?></h4> <label><input type="radio" value="<?php echo $ans_array[0]; ?>" name="<?php echo $question; ?>"> <?php echo $ans_array[0]; ?></label> <label><input type="radio" value="<?php echo $ans_array[1]; ?>" name="<?php echo $question; ?>"> <?php echo $ans_array[1]; ?></label> <label><input type="radio" value="<?php echo $ans_array[2]; ?>" name="<?php echo $question; ?>"> <?php echo $ans_array[2]; ?></label> <?php } ?> <form action="ex5-1.php" method="POST"> <input type="submit" value="Submit" name="submit"> </form> <p>Total Score</p> </body> </html>