前提・実現したいこと
クイズアプリを開発し、設問をランダムに表示させたいです。
###発生している問題・エラーメッセージ
・正解の選択肢を選択しても、不正解として処理されます。
・同じ問題が繰り返し表示されます。
###ソースコード
index.php
PHP
1<?php 2// 外部ファイル参照 3require_once(__DIR__ . '/config.php'); 4require_once(__DIR__ . '/Quiz.php'); 5require_once(__DIR__ . '/functions.php'); 6require_once(__DIR__ . '/AccessLog.php'); 7// インスタンス化 8$db = new Quiz(); 9 10// 初期化 11$data = $db->DisplayQuestion(); 12$array = $db->DisplayChoices(); 13/* 14$tmp = $db->test(); 15var_dump($tmp); 16*/ 17// 選択肢をシャッフルする 18shuffle($array); 19 20?> 21 22<!DOCTYPE html> 23<html lang="ja"> 24<head> 25 <meta charset="utf-8"> 26 <title>Quiz</title> 27 <!-- Required meta tags --> 28 <meta charset="utf-8"> 29 <meta name="viewport" content="width=device-width,initial-scale=1"> 30 <!-- Bootstrap CSS --> 31 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous"> 32 <link rel="stylesheet" href="styles.css"> 33</head> 34<body> 35<header> 36 <div class="container"> 37 <h1>English Quiz</h1> 38 </div> 39</header> 40 <?php if ($db->isFinished()) : ?> 41 <div class="container"> 42 <div id="result"> 43 Your score ... 44 <!-- <div><?= h($db->getScore()); ?> %</div> --> 45 <div><?= h($db->getScore()); ?> %</div> 46 47 </div> 48 <a href=""><div id="btn">Replay?</div></a> 49 </div> 50 <?php $db->reset(); ?> 51 52 <?php else : ?> 53<div id="container"> 54 <p class="h1"><?= h($data); ?></p> 55 <ul class="list-group"> 56 <?php foreach ($array as $a) : ?> 57 <li class="list-group-item answer"><?= $a; ?></li> 58 <?php endforeach; ?> 59 </ul> 60 <div id="btn" class="disabled"><?= $db->isLast() ? 'Show result' : 'Next Question'; ?></div> 61 </div> 62 <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> 63 <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> 64 <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script> 65 66 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 67 <script src="quiz.js"></script> 68 <?php endif; ?> 69 </body> 70</div> 71</body> 72</html>
Quiz.php
PHP
1<?php 2 3//namespace MyApp; 4 5class Quiz { 6 7 private $_db; 8 private $_obj_to_array; 9 private $_quiz; 10 11 public function __construct() { 12 $this->_setup(); 13 14 if (!isset($_SESSION['current_num'])) { 15 $this->_initSession(); 16 } 17 } 18 19 private function _setup() { 20 define('DB_DATABASE', '******'); 21 define('DB_USERNAME', '******'); 22 define('DB_PASSWORD', '******'); 23 define('PDO_DSN', 'mysql:host=******.ne.jp;dbname=' . DB_DATABASE); 24 25 try { 26 $this->_db = new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD); 27 $query_result = $this->_db->query("SELECT * FROM qa_table ORDER BY RAND()"); 28 // データベースの全フィールドを抽出する 29 $db_data = $query_result->fetchAll(\PDO::FETCH_OBJ); 30 // オブジェクトから配列に変換する 31 $this->_obj_to_array = json_decode(json_encode($db_data), true); 32 $this->_quiz = explode(",", $this->_obj_to_array[$_SESSION['current_num']]['choices']); 33 34 } catch(PDOEXception $e) { 35 echo $e->getMessage(); 36 exit; 37 } 38 } 39 40 private function _initSession() { 41 $_SESSION['current_num'] = 0; 42 $_SESSION['correct_count'] = 0; 43 } 44 45 public function DisplayQuestion() { 46 return $this->_obj_to_array[$_SESSION['current_num']]['question']; 47 } 48 49 public function DisplayChoices() { 50 return $this->_quiz; 51 } 52 53 public function checkAnswer() { 54 $correctAnswer = $this->_quiz['0']; 55 if ($correctAnswer === $_POST['answer']) { 56 $_SESSION['correct_count']++; 57 } 58 $_SESSION['current_num']++; 59 return $correctAnswer; 60 } 61 62 public function isLast() { 63 return count($this->_obj_to_array) === $_SESSION['current_num'] + 1; 64 } 65 66 public function isFinished() { 67 return count($this->_obj_to_array) === $_SESSION['current_num']; 68 } 69 70 public function getScore() { 71 return round($_SESSION['correct_count']/count($this->_obj_to_array)*100); 72 } 73 public function reset() { 74 $this->_initSession(); 75 } 76/* 77 public function test() { 78 return $this->_obj_to_array; 79 } 80*/ 81}
_answer.php
PHP
1<?php 2 3require_once(__DIR__ . '/config.php'); 4require_once(__DIR__ . '/Quiz.php'); 5 6$db = new Quiz(); 7$correctAnswer = $db->checkAnswer(); 8 9// PHPで値をJSON形式で表示する 10header('Content-Type: application/json; charset=UTF-8'); 11echo json_encode([ 12 'correct_answer' => $correctAnswer 13]);
###試した事
Quiz.phpで、以下の様にshuffle($this->_obj_to_array); を修正しましたが、結果変わりませんでした。
PHP
1<?php 2 3//namespace MyApp; 4 5class Quiz { 6 7 private $_db; 8 private $_obj_to_array; 9 private $_quiz; 10 11 public function __construct() { 12 $this->_setup(); 13 14 if (!isset($_SESSION['current_num'])) { 15 $this->_initSession(); 16 } 17 } 18 19 private function _setup() { 20 define('DB_DATABASE', '******'); 21 define('DB_USERNAME', '******'); 22 define('DB_PASSWORD', '******'); 23 define('PDO_DSN', 'mysql:host=******.ne.jp;dbname=' . DB_DATABASE); 24 25 try { 26 $this->_db = new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD); 27 $query_result = $this->_db->query("SELECT * FROM qa_table"); 28 // データベースの全フィールドを抽出する 29 $db_data = $query_result->fetchAll(\PDO::FETCH_OBJ); 30 // オブジェクトから配列に変換する 31 $this->_obj_to_array = json_decode(json_encode($db_data), true); 32 shuffle($this->_obj_to_array); // 変更 33 $this->_quiz = explode(",", $this->_obj_to_array[$_SESSION['current_num']]['choices']); 34 35 } catch(PDOEXception $e) { 36 echo $e->getMessage(); 37 exit; 38 } 39 } 40 41 private function _initSession() { 42 $_SESSION['current_num'] = 0; 43 $_SESSION['correct_count'] = 0; 44 } 45 46 public function DisplayQuestion() { 47 return $this->_obj_to_array[$_SESSION['current_num']]['question']; 48 } 49 50 public function DisplayChoices() { 51 return $this->_quiz; 52 } 53 54 public function checkAnswer() { 55 $correctAnswer = $this->_quiz['0']; 56 if ($correctAnswer === $_POST['answer']) { 57 $_SESSION['correct_count']++; 58 } 59 $_SESSION['current_num']++; 60 return $correctAnswer; 61 } 62 63 public function isLast() { 64 return count($this->_obj_to_array) === $_SESSION['current_num'] + 1; 65 } 66 67 public function isFinished() { 68 return count($this->_obj_to_array) === $_SESSION['current_num']; 69 } 70 71 public function getScore() { 72 return round($_SESSION['correct_count']/count($this->_obj_to_array)*100); 73 } 74 public function reset() { 75 $this->_initSession(); 76 } 77/* 78 public function test() { 79 return $this->_obj_to_array; 80 } 81*/ 82}