Я не могу понять, что не так с этим кодом. Я думал, что его точки с запятой, но я добавил его везде. Он выдает ошибку, когда я посещаю этот код в браузере, говоря, что не смог обработать запрос. Может кто-нибудь сказать, пожалуйста, где я допустил ошибку? Я попытался установить переменные свойства только в ненулевые значения (просто объявив их), но это не сработало.
<?php
class Controller {
/*add number of comments to quizComments tables'
*/
$dbConnection = null;
$userName = null;
$userScore = 0;
$currentQuestionIndex = 1;
function connectToDb() {
$mysql = 'mysql:dbname=cw;host=localhost';
$username = 'root';
$password = '';
try {
$this->dbConnection = new PDO($mysql, $username, $password);
echo 'connected to database';
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
};
function getComments ($commentTable, $questionID) {
$this->dbConnection->query("SELECT * FROM ('$commentTable') WHERE question_id = ('$questionID')");
};
function getQuestion ($questionTable, $questionID) {
$this->dbConnection->query("SELECT * FROM ('$questionTable') WHERE id = ('$questionID')");
};
function addComment ($commentTable, $questionID, $author, $content, $emailContact, $dateAdded) {
$this->dbConnection->query("INSERT INTO ('$commentTable') (question_id) VALUES ('$questionID')");
echo 'Thank you for your comment. We will approve it soon.';
};
function addQuestion ($questionTable, $question, $answer1, $answer2, $answer3, $answer4, $correctAnswerIndex, $explanation, $author, $shortDesc, $dateAdded) {
$this->dbConnection->query("INSERT INTO ('$questionTable') (question_id) VALUES ('$questionID')");
echo 'Thank you for submitting. We will approve it soon.';
};
function progressToNextQuestion($questionTable) {
$question = $this->getQuestion($questionTable, $this->currentQuestionIndex);
};
function startQuiz ($questionTable) {
if ($questionTable === 'QuizQuestionsBanking') {
$question = $this->getQuestion($questionTable, $this->currentQuestionIndex);
};
if ($questionTable === 'QuizQuestionsTrading') {
$question = $this->getQuestion($questionTable, $this->currentQuestionIndex);
};
};
function checkAnswer ($questionTable, $questionID, $chosenIndex) {
$question = $this->getQuestion($questionTable, $questionID);
$correctAnswerIndex = $question['solutionIndex'];
if ($correctAnswerIndex === $chosenIndex) {
$this->score++;
$this->currentQuestionIndex++;
$this->saveCurrentScore();
$this->progressToNextQuestion($questionTable);
} else {
echo 'Wrong. Please try again.';
};
};
function saveCurrentScore () {
/* 1. make table called 'Leaderboard'
* 2. insert into table 'username,score' values (username,score)
*/
};
function get15MostRecentQuestion () {
/*sort descending, date */
};
function get15HighestRatedQuestion () {
/* show different metric from usual: divide rating over # of votes*/
/* sort descending, rating
*
* */
};
function get15MostCommentedQuestion () {
/* sort number_of_comments descending
*/
};
function getTop10Contributors() {
};
function rateQuestion () {
};
function getLeaderboard() {
/* SELECT * from Leaderboard
*/
};
};
?>
Это неверный код, вам не нужно все ;
и вы не определили области свойств класса. включить ini_set('error_reporting', E_ALL)
в процессе разработки вы можете увидеть свои фатальные синтаксические ошибки.
<?php
class Controller {
/*add number of comments to quizComments tables'
*/
public $dbConnection = null;
public $userName = null;
public $userScore = 0;
public $currentQuestionIndex = 1;
function connectToDb() {
$mysql = 'mysql:dbname=cw;host=localhost';
$username = 'root';
$password = '';
try {
$this->dbConnection = new PDO($mysql, $username, $password);
echo 'connected to database';
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
function getComments ($commentTable, $questionID) {
$this->dbConnection->query("SELECT * FROM ('$commentTable') WHERE question_id = ('$questionID')");
}
function getQuestion ($questionTable, $questionID) {
$this->dbConnection->query("SELECT * FROM ('$questionTable') WHERE id = ('$questionID')");
}
function addComment ($commentTable, $questionID, $author, $content, $emailContact, $dateAdded) {
$this->dbConnection->query("INSERT INTO ('$commentTable') (question_id) VALUES ('$questionID')");
echo 'Thank you for your comment. We will approve it soon.';
}
function addQuestion ($questionTable, $question, $answer1, $answer2, $answer3, $answer4, $correctAnswerIndex, $explanation, $author, $shortDesc, $dateAdded) {
$this->dbConnection->query("INSERT INTO ('$questionTable') (question_id) VALUES ('$questionID')");
echo 'Thank you for submitting. We will approve it soon.';
}
function progressToNextQuestion($questionTable) {
$question = $this->getQuestion($questionTable, $this->currentQuestionIndex);
}
function startQuiz ($questionTable) {
if ($questionTable === 'QuizQuestionsBanking') {
$question = $this->getQuestion($questionTable, $this->currentQuestionIndex);
}
if ($questionTable === 'QuizQuestionsTrading') {
$question = $this->getQuestion($questionTable, $this->currentQuestionIndex);
}
}
function checkAnswer ($questionTable, $questionID, $chosenIndex) {
$question = $this->getQuestion($questionTable, $questionID);
$correctAnswerIndex = $question['solutionIndex'];
if ($correctAnswerIndex === $chosenIndex) {
$this->score++;
$this->currentQuestionIndex++;
$this->saveCurrentScore();
$this->progressToNextQuestion($questionTable);
} else {
echo 'Wrong. Please try again.';
}
}
function saveCurrentScore () {
/* 1. make table called 'Leaderboard'
* 2. insert into table 'username,score' values (username,score)
*/
}
function get15MostRecentQuestion () {
/*sort descending, date */
}
function get15HighestRatedQuestion () {
/* show different metric from usual: divide rating over # of votes*/
/* sort descending, rating
*
* */
}
function get15MostCommentedQuestion () {
/* sort number_of_comments descending
*/
}
function getTop10Contributors() {
}
function rateQuestion () {
}
function getLeaderboard() {
/* SELECT * from Leaderboard
*/
}
}
Примечание Не инициализируйте вашу базу данных в ваших контроллерах, иначе все ваши контроллеры будут иметь такой же код. Кроме того, он нигде не вызывается, что также может быть проблемой; p
Здесь вопрос, который вы пропустили access specifiers
для переменных класса
$dbConnection = null;
$userName = null;
$userScore = 0;
$currentQuestionIndex = 1;
в
public $dbConnection = null;
public $userName = null;
public $userScore = 0;
public $currentQuestionIndex = 1;
и в php };
неправильный код замените его на}