为什么我的PHP类没有渲染?

时间:2017-08-16 09:41:01

标签: php

我无法弄清楚这段代码有什么问题。我想它的分号,但我到处都添加了它。当我在浏览器中访问此代码无法处理请求时,它会引发错误。有人可以告诉我,我在哪里弄错了吗?我尝试将属性变量设置为非空值(只是声明它们),但这不起作用。

<?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
  */
  };

};

?>

2 个答案:

答案 0 :(得分:0)

它的代码无效,您不需要所有;,并且您尚未定义类属性范围。在开发过程中启用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

答案 1 :(得分:0)

此问题是您错过了access specifiers类变量

  $dbConnection = null;
  $userName = null;
  $userScore = 0;
  $currentQuestionIndex = 1;

  public $dbConnection = null;
  public $userName = null;
  public $userScore = 0;
  public $currentQuestionIndex = 1;

并且在php };中不是有效的代码,用}

替换它