HTML and CSS Reference
In-Depth Information
Saving New Questions
In order to save a new question to the database, both the room ID and the new question text (as a string) are passed to
the create_question() method. The first query inserts the question into the questions table and then stores the ID
of the newly saved question in $question_id .
Next, the first vote for the question—because the user who asked it counts as the first vote—is added to the
question_votes table using the newly created question ID.
Implement this method by adding the following bold code to Question_Model :
public function get_room_questions( $room_id )
$sql = "SELECT
id AS question_id,
FROM questions
LEFT JOIN question_votes
ON( = question_votes.question_id )
WHERE room_id = :room_id
ORDER BY is_answered, vote_count DESC";
$stmt = self::$db->prepare($sql);
$stmt->bindParam(':room_id', $room_id, PDO::PARAM_INT);
$questions = $stmt->fetchAll(PDO::FETCH_OBJ);
return $questions;
* Stores a new question with all the proper associations
* @param $room_id int The ID of the room
* @param $question string The question text
* @return array The IDs of the room and the question
public function create_question( $room_id, $question )
// Stores the new question in the database
$sql = "INSERT INTO questions (room_id, question)
VALUES (:room_id, :question)";
$stmt = self::$db->prepare($sql);
$stmt->bindParam(':room_id', $room_id);
$stmt->bindParam(':question', $question);
// Stores the ID of the new question
$question_id = self::$db->lastInsertId();
Search WWH ::

Custom Search