クイズ作成の続き、データ入力とクイズの結果を表示を作りました。
1.データ入力 スクリーン
コードは 以下のようになります。
Submitを押すとadd.phpのphp部分、if(isset($_POST[‘submit]))がTrueになるのでその部分が実行されます。
まず4つの選択肢をArrayに入れて処理しやすくします。
次に、questionテーブルへのInsert分を構成し実行します。それがうまく行ったらchoicesのInsertをFor Eachのループで実行します。(選択肢をArrayに入れたのはこの為)
最後の選択肢のInsertがうまくいったら$msgをセットして、Add a questionの下に表示させます。
全部で何問あるかの表示もしているのでquestionテーブルを読んでカウントもしています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
<?php include 'database.php'; ?> <?php if(isset($_POST['submit'])){ //Get post variables $question_number = $_POST['question_number']; $question = $_POST['question']; $correct_choice = $_POST['correct_choice']; //Choices array $choices = array(); $choices[1] = $_POST['choice1']; $choices[2] = $_POST['choice2']; $choices[3] = $_POST['choice3']; $choices[4] = $_POST['choice4']; //Question query $query = "INSERT INTO `question`(question_id, question) VALUES( '$question_number','$question')"; //Run query $insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__); //Validate insert if($insert_row){ foreach($choices as $choice => $value){ if($value != ''){ if($correct_choice == $choice){ $is_correct = 1; } else { $is_correct = 0; } //Choice query $query = "INSERT INTO `choices` (question_id, is_correct, choice) VALUES ('$question_number','$is_correct','$value')"; //Run query $insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__); //Validate insert if($insert_row){ continue; } else { die('Error : ('.$mysqli->errno . ') '. $mysqli->error); } } } $msg = 'Question has been added'; } } /* * Get total questions */ $query = "SELECT * FROM `question`"; //Get The Results $questions = $mysqli->query($query) or die($mysqli->error.__LINE__); $total = $questions->num_rows; $next = $total+1; ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>CAPM Quiz</title> <meta name="viewport" content="width=device-width, initial-scale=1.0 "><!--setup viewport --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <link rel="stylesheet" href="css/style.css"><!--Obtain the stylesheet--> </head> <body> <header> <div class="container"> <h1>Welcome to CAPM Quiz</h1> </div> </header> <main> <div class="container"> <h2>Add A Question</h1> <?php if(isset($msg)){ echo '<p>'.$msg.'</p>'; } ?> <form method="post" action="add.php"> <p> <label>Question Number: </label> <input type="number" value="<?php echo $next; ?>" name="question_number" /> </p> <p> <label>Question Text: </label> <input type="text" name="question" /> </p> <p> <label>Choice #1: </label> <input type="text" name="choice1" /> </p> <p> <label>Choice #2: </label> <input type="text" name="choice2" /> </p> <p> <label>Choice #3: </label> <input type="text" name="choice3" /> </p> <p> <label>Choice #4: </label> <input type="text" name="choice4" /> </p> <p> <label>Correct Choice Number: </label> <input type="number" name="correct_choice" /> </p> <p> <input type="submit" name="submit" value="Submit" /> </p> </form> </div> </main> <footer> <div class="container"> Copyright © 2019, CAPM Quiz </div> </footer> </body> </html> |
2.クイズ結果表示
2-1.process.phpの一部で、クイズに正解しなかった時にincorrectテーブルに追加するようにしました。
コメントアウトされているのは、$_SESSIONに不正解のquestion_idとchoice_idをArrayとして保存したかったのですが、うまく行かなかったなごりです。
このprocess.phpでなくテスト用にこれだけ取り出して行うとうまくいきますが、ここではなぜかうまく行きませんでしたので、あきらめてテーブルに保存することにしました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
if($correct_choice == $selected_choice){ //Answer is correct $_SESSION['score']++; } else { //Choice query $today = date("Y/m/d"); $query = "INSERT INTO `incorrect` (question_id, correct_choice, incorrect_choice,testdate) VALUES ('$number','$correct_choice','$selected_choice', '$today' )"; //Run query $insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__); //array_push($_SESSION, array('question id' => $number)); //array_push($_SESSION, array('correct choice' => $correct_choice)); //$_SESSION['question id'][] = $number; //$_SESSION['correct choice'][] = $correct_choice; //array_push($_SESSION['question id'], $number); //array_push($_SESSION['correct choice'], $correct_choice); } |
2-2.final.phpです。
このphpでは不正解のデータを保存したincorrectテーブルから読んで問題と選択肢を表示させています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?php include 'database.php'; ?> <?php session_start(); ?> <?php $query = "SELECT testdate, question, c.choice as correct_choice , d.choice as incorrect_choice FROM question q inner join incorrect i on i.question_id = q.question_id inner join choices c on c.choice_id = correct_choice inner join choices d on d.choice_id = incorrect_choice"; // Get result $incorrect = $mysqli->query($query) or die($mysqli->error.__LINE__); ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>CAPM Quiz</title> <meta name="viewport" content="width=device-width, initial-scale=1.0 "><!--setup viewport --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <link rel="stylesheet" href="css/style.css"><!--Obtain the stylesheet--> </head> <body> <header> <div class="container"> <h1>Welcome to CAPM Quiz</h1> <div class="row"> <div class="col-xs-12 main"> <h2>Test Result</h2> <p>Final Score: <?php echo $_SESSION['score']; ?>/<?php echo $_SESSION['total'];?></p> <table class="table table-striped table-bordered table-hover"> <tr class="bg-primary"> <th>Test Date</th> <th>Question</th> <th>Correct answer</th> <th>Your answer</th> </tr> <?php while($row = $incorrect->fetch_assoc()): ?> <tr> <td><?php echo $row['testdate']; ?></td> <td><?php echo $row['question']; ?></td> <td><?php echo $row['correct_choice']; ?></td> <td><?php echo $row['incorrect_choice']; ?></td> </tr> <?php endwhile; ?> </table> <a href="question.php?n=1" class="start">Take again</a> </div><!--col-xs-12--> </div><!--row--> </div><!--container--> </header> <footer> <div class="container"> Copyright © 2019, CAPM Quiz </div> </footer> </body> </html> |
まとめると、
Session variableとArrayの相性が悪いのか、process.phpの何かがよくないのかとにかくArray_pushを実行しても何もデータが入ってこないという現象には苦労しました。
結局原因は分からないですが、執着しても仕方ないのでテーブル作成に切り替えてうまく行ったので良かったです。