weixin_33716941 2015-02-10 00:34 采纳率: 0%
浏览 10

PHP Ajax类系统

I'm working on a like / dislike system for my websites commenting system.

I was working off of this tutorial, http://www.webcodo.net/like-dislike-system-with-jquery-ajax-and-php-youtube-like-design/

Except I have multiple like/dislike buttons on every page, because there's multiple comments people have the ability to like/dislike.

I'm brand new to ajax btw.

Here's the javascript file.

$('.like-btn').click(function(){
    $('.dislike-btn').removeClass('dislike-h');    
    $(this).addClass('like-h');
    $.ajax({
        type:"POST",
        url:"../outfits/outfit.php",
        data:'act=like&function=true&pageID=' + pageID + '&user=' + email,
        success: function(){
        }
    });
});

$('.dislike-btn').click(function(){
    $('.like-btn').removeClass('like-h');
    $(this).addClass('dislike-h');
    $.ajax({
        type:"POST",
        url:"../outfits/outfit.php",
        data:'act=dislike&function=true&pageID=' + pageID + '&user=' + email,
        success: function(){
        }
    });
});

This is the "comment row"

<div class="row">
                    <h5>Comments</h5>
                    <div class="row">
                        <div class="small-12 columns">
                            <textarea rows="4" class="post-comment" placeholder="You can comment or rate without doing the other" name="comment" maxlength="140"></textarea>
                        </div>
                    </div>
                    <div class="row">
                        <div class="small-10 large-10 columns">
                            <div class="rating">
<input type="radio" id="star5" name="rating" value="5" /><label for="star5">5 stars</label>
<input type="radio" id="star4" name="rating" value="4" /><label for="star4">4 stars</label>
<input type="radio" id="star3" name="rating" value="3" /><label for="star3">3 stars</label>
<input type="radio" id="star2" name="rating" value="2" /><label for="star2">2 stars</label>
<input type="radio" id="star1" name="rating" value="1" /><label for="star1">1 star</label>
                            </div>
                        </div>
                        <div class="small-2 large-2 columns">
                            <input type="submit" name="postComment" class="small button radius login-button right" value="Post">
                        </div>
                    </div>
                    <div class="row">
                        <div class="small-12 columns">
                            <hr />
                        </div>
                    </div>
                    <?php
                        foreach ($commentRows as $commentRow) {
                                try {
                                $pageID = $commentRow['id']; // The ID of the page, the article or the video ...

                                    extract($_POST);

                                    $dislike_sql = 'SELECT * FROM  likes WHERE user = "'.$email.'" and comment_id = "'.$pageID.'" and rate = 2 ';
                                    $stmt = $dbhandle->prepare($dislike_sql);
                                    $stmt->execute();

                                    $dislike_count = $stmt->rowCount();


                                    $like_sql = 'SELECT * FROM  likes WHERE user = "'.$email.'" and comment_id = "'.$pageID.'" and rate = 1 ';

                                    $stmt = $dbhandle->prepare($like_sql);
                                    $stmt->execute();

                                    $like_count = $stmt->rowCount();


                                    if($act == 'like'): //if the user click on "like"
                                    if(($like_count == 0) && ($dislike_count == 0)){

                                        $rate = 1;
                                        $sql = 'INSERT INTO likes (comment_id, user, rate )VALUES(:pageID, :email, :rate)';

                                        //prepare data for insert
                                        $stmt = $dbhandle->prepare($sql);

                                        $stmt->bindValue(":pageID", $pageID);
                                        $stmt->bindValue(":email", $email);
                                        $stmt->bindValue(":rate", $rate);

                                        //Execute
                                        $stmt->execute();
                                    }
                                    if($dislike_count == 1){

                                        $rate = 1;

                                        $sql = 'UPDATE likes SET rate = ? WHERE comment_id = ? and user = ?';

                                        $stmt = $dbhandle->prepare($sql);
                                        $stmt->execute(array($rate, $pageID, $email));
                                    }
                                    endif;

                                    if($act == 'dislike'): //if the user click on "like"
                                    if(($like_count == 0) && ($dislike_count == 0)){

                                        $rate = 2;
                                        $sql = 'INSERT INTO likes (comment_id, user, rate )VALUES("'.$pageID.'", "'.$email.'", "2")';

                                        //prepare data for insert
                                        $stmt = $dbhandle->prepare($sql);

                                        $stmt->bindValue(":pageID", $pageID);
                                        $stmt->bindValue(":email", $email);
                                        $stmt->bindValue(":rate", $rate);

                                        //Execute
                                        $stmt->execute();
                                    }
                                    if($like_count == 1){

                                        $rate = 2;
                                        $sql = 'UPDATE likes SET rate = ? WHERE comment_id = ? and user = ?';

                                        $stmt = $dbhandle->prepare($sql);
                                        $stmt->execute(array($rate, $pageID, $email));
                                    }
                                    endif;
                                }
                                catch (PDOException $e) {
                                    echo $e->getMessage();   
                                }
                                print '<div class="row comment-row" id="gohere">
                                <div class="small-12 columns">
                                    <div class="small-12 columns">
                                        <div class="row">
                                            <div class="small-12 large-2 columns text-center">
                                                <div class="row">
                                                    <div class="small-12 columns">
                                                        <span>' . $commentRow['first-name'] . '</span>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="small-12 columns">
                                                        <img src="https://a3-images.myspacecdn.com/images03/1/240e42b5d9ce48a78983961e7fcb3c39/300x300.jpg" class="profile-pic-comment" />
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="small-12 large-10 columns comments">
                                                <div class="small-12 columns comment-text">
                                                    <span>' . $commentRow['text'] . '</span>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="small-12 large-4 columns right">
                                                    <div class="small-6 columns">
                                                        <div class="like-btn' . $commentRow['id'] . ' ';
                                                        if($like_count == 1){
                                                            echo 'like-h';
                                                        }

                                                        print '">Like</div>
                                                    </div>
                                                    <div class="small-6 columns">
                                                        <div class="dislike-btn' . $commentRow['id'] . ' ';

                                                        if($dislike_count == 1){
                                                            echo 'dislike-h';
                                                        }
                                                        print '"></div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>';
                            }
                    ?>
                </div>

Right now I can see the counts of the likes / dislikes, which is all zero because the insert and update queries are not working. I put in data manually to see if the count was working.

Any help would be greatly appreciated. Let me know if I need to post anymore details/code.

  • 写回答

1条回答 默认 最新

  • weixin_33694172 2015-02-10 02:05
    关注

    You code try for count() instead using rowCount() function.

    $CountLikes = count($smpt);
    

    I don't think that's the issue. Anyways, since you said you are new to AJAX, have you tried using the like system without AJAX. Not saying, the problem lies in the AJAX, but it will either rule out whether or not your issue is Server-Side or if it's an AJAX issue. Ruling out where the issue is will help you a lot.

    If it works successfully without the AJAX, then we can assume that AJAX is not sending the data to the server correctly. You can work from there.

    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试