2017-08-16 9 views
-1

このコードで何が問題なのかわかりません。私はそのセミコロンを考えましたが、どこにでも追加しました。リクエストを処理できないというこのコードをブラウザに表示すると、エラーが発生します。誰かが私にどこでミスをしたのか教えてもらえますか?私はプロパティ変数をヌル以外の値に設定しようとしましたが(単にそれらを宣言しています)、それはうまくいきませんでした。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 
    */ 
    }; 

}; 

?> 
+0

と交換してください、あなたはあなたのウェブサーバのエラーログに取得エラーを投稿することができますか? – Fredster

+1

プロパティを正しく宣言する必要があります。* public、protected、privateのいずれかのキーワードを使用して定義され、その後に通常の変数宣言*(マニュアルを参照)が続きます。 – jeroen

+1

カスタムコンストラクタはありません。構文上のエラーがない限り、それは問題ありません。あなたの質問から欠けているものは、あなたがこのクラスを呼び出す方法と、あなたが得る正確なエラーです。 –

答えて

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

+0

ありがとうございます。関数のアクセスプロパティを宣言する必要がありますか? – chemicalre

+0

あなたはそうすることができますが、パブリックメソッドでは必要ありません。 –

0

問題は、あなたは

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

にクラス変数のためのaccess specifiers

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

を逃し、PHP };に有効なコードではありません}

+0

';'もすべて構文エラーです。 –

+0

はい、正しいです... – kranthi

関連する問題