I am creating an application similar to stackoverflow in which it has ratings for questions and answers, and I also have tabs that show comments on the oldest, newest and voices. I have problems sorting by voice.
Here is my function:
public function getComments($threadid, $tab = 'oldest', $voting = null) {
if ($tab == 'oldest') {
$sql = "SELECT * FROM comments WHERE threadid = :threadid ORDER BY date ASC";
} else if ($tab == 'newest') {
$sql = "SELECT * FROM comments WHERE threadid = :threadid ORDER BY date DESC";
} else if ($tab == 'votes') {
} else {
$sql = "SELECT * FROM comments WHERE threadid = :threadid ORDER BY date ASC";
}
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':threadid', $threadid);
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $row;
}
Here is my database design:
**comments:** | id | userid | threadid | message | date |
**commentsrating:** | userid | commentid | voteup | votedown |
If the ratings are in a separate table from the comments, is it possible to make a request for $tab == 'votes'which will match the rest of the code?
and finally HTML:
<?php
if (isset($_GET['tab'])) {
$getComments = $thread->getComments($threadid, $_GET['tab'], $voting);
} else {
$getComments = $thread->getComments($threadid);
} ?>
<?php for ($i = 0; $i < count($getComments); $i++) { ?>
<p>
<?php echo $getComments[$i]['message']; ?>
</p>
<p>
<span class="bid_votes_count" id="bid_votes_count<?php echo $getComments[$i]['id'] ?>">
<?php echo $voting->getEffectiveCommentVotes($getComments[$i]['id']) . " votes"; ?>
</span>
<span class="bid_vote_buttons" id="bid_vote_buttons<?php echo $getComments[$i]['id'] ?>">
<a href="javascript:;" class="bid_vote_up" id="<?php echo $getComments[$i]['id'] ?>"></a>
<a href="javascript:;" class="bid_vote_down" id="<?php echo $getComments[$i]['id'] ?>"></a>
</span>
</p>
<?php } ?>
Thanks in advance!
source
share