Sıralama oyla comments?

0 Cevap php

Ben soruları ve cevapları için derecelendirme sahip olmasıdır StackOverflow benzer ve ben de yeni ve oy, eski tarafından yorumlarınızı göstermek sekmeler var bir uygulama yapıyorum. Sorun oylarıyla sıralama sahip Im.

İşte benim fonksiyonudur:

 /**
     *
     * @param int $threadid
     * @param string $tab
     * @param object $voting referencing Voting.class.php (may not be needed)
     * @return database querry/array 
     */
    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') {
            //i dont know what to do here? read below for more explanation
        } 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;
    }

İşte benim veritabanı tasarımı:

**comments:** | id | userid | threadid | message | date |

**commentsrating:** | userid | commentid | voteup | votedown |

Derecelendirme yorumlardan ayrı bir tabloda iseniz $tab == 'votes' bu kodu geri kalanı ile uyumlu olacak bir sorgu yapmak mümkün mü?

ve son olarak HTML:

<?php //Get comments
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!

0 Cevap