2011-12-20 53 views
0

我怎样才能得到我需要的变量值,我已经通过MySQL选择了评论和喜欢的值,并且创建了一个类似的按钮,但是当我点击它时,它会添加类似于上次选择/发布的变量值,因此它喜欢上次生成的评论。如何让它像我想要的而不是最后选择的那个?我怎样才能得到我需要的变量值

实施例:

我有ID的评论(1,2,3)时产生其预定他们以该顺序(1,2,3)时,我想用ID评论= 2它喜欢的评论3,因为它是最后生成的,所以在我喜欢评论2时,变量ID值为3.

我没有任何意义,我希望你们能够得到它,请帮助吗?

回答

0

note.php

//-------- WHILE LOOP FOR GETTING NOTE COMMENTS ------- 

$commentsList = array(); 

$query = mysql_query("SELECT * FROM note_comments WHERE note_id='$note_id'"); 
while (($row = mysql_fetch_assoc($query)) != false) { 
    $commentsList[] = array(
     'comId' => $row['id'], 
     'comment' => $row['comment'], 
     'com_likes' => $row['likes'], 
     'com_likers' => $row['likers'] 
    ); 
} 

note.php(主体):

 <div id="noteComments"> 
     <h2>Comments</h2> 
     <?php 
     $commentL = $commentsList; 
     if (count($commentL) == 0) { 
      echo 'Sorry, there are no comments.'; 
     } else { 
      echo '<ul style="list-style:none;">'; 
      foreach($commentL as $comment) { 
       echo '<li><p>', $comment['comment'], '</p><p><a style="background-color:#3b3c3b; color:#fff; 
       text-decoration:none; padding: 1 5 1 5; border-radius:5px; border: 1px solid black; " href="#" onclick="like_add(', $comment['comId'] ,');">Like</a><span id="comment_', $comment['comId'] ,'_likes">', $comment['com_likes'] , '</span> like this</p></li>'; 
      } 
      echo '</ul>'; 
     } 
     ?> 
     </div><!-- end noteComments --> 

like.js

function like_add(comId) { 
$.post('ajax/like_add.php', {comId:comId}, function(data) { 
    if (data == 'success') { 
     like_get(comId); 
    } else { 
     alert(data); 
    } 
}); 
} 

function like_get(comId) { 
$.post('ajax/like_get.php', {comId:comId}, function(data) { 
    $('#comment_'+comId+'_likes').text(data); 
}); 
} 

like_add.php

<?php 
include_once("../scripts/checkuserlog.php"); 
include '../functions/like.php'; 
if (isset($_POST['comId']) && comment_exists($_POST['comId'])) { 
$comId = $_POST['comId']; 
add_like($comId); 
} 
?> 

like_get.php


like.php

<?php 
function comment_exists($comId) { 
$comId = (int)$comId; 
return (mysql_result(mysql_query("SELECT COUNT('id') FROM note_comments WHERE id=$comId"), 0) == 0) ? false : true; 
} 

function like_count($comId) { 
$comId = (int)$comId; 
return (int)mysql_result(mysql_query("SELECT likes FROM note_comments WHERE id=$comId"), 0, 'likes'); 
} 
function add_like($comId) { 
$comId = (int)$comId; 
$visitor = $_SESSION['id']; 
$com_likers = ""; 
$query = mysql_query("SELECT likes, likers FROM note_comments WHERE id='$comId'"); 
while (($row = mysql_fetch_array($query)) != false) { 

     $com_likes = $row['likes']; 
     $com_likers = $row['likers']; 
} 
$thisComlikers = explode(",", $com_likers); 
if (!in_array($visitor, $thisComlikers)) { 
    if ($com_likers != "") { $com_likers = "$com_likers,$visitor"; } else { $com_likers = "$visitor"; } 

mysql_query("UPDATE note_comments SET likes = likes + 1 WHERE id=$comId")or die (mysql_error()); 
mysql_query("UPDATE note_comments SET likers =$com_likers WHERE id=$comId")or die (mysql_error()); 
echo 'success'; 
} else { 
    echo 'You have already liked this!'; 
} 
} 

?> 
1

比方说,我们都在同一个岗位三点意见:

第一个表:评论

id | post_id | comment 
1 | 1  | "something" 
2 | 1  | "something else" 
3 | 1  | "something else entirely" 

二表:comment_likes

id | comment_id 
1 | 2 
2 | 2 
3 | 3 

在这个例子中,注释2将有2个喜欢,并且评论3个。

这可能是显示我们评论的代码。这里的诀窍是为类似链接添加一个自定义属性,以便我们可以在javascript中检测评论ID。

<div class="comments"> 
<?php 
$res = mysql_query('SELECT * FROM `comments` WHERE `post_id` = '.$post_id.';'); 
while ($row = mysql_fetch_array($res)) { 
    $cid = $row['id']; 
    echo '<div class="comment">'; 
    echo $row['comment'].'<br />'; 
    echo '<span class="like" comment-id="'.$cid.'">Like</span>'; 
    $lres = mysql_query('SELECT COUNT(*) FROM `comment_likes` WHERE `comment_id` = '.$cid.';'); 
    $likes = mysql_result($lres,0); 

    echo '<span class="likes">'; 
    if ($likes > 0) { 
     echo '<span class="num_likes">'.$likes.'</span>'; 
     $p = ($likes > 1) ? 'people like' : 'person likes'; 
     echo $likes.' '.$p.' this'; 

    } 
    echo '</span>'; 
    echo '</div>'; 
} 
?> 
</div> 

现在的JavaScript(使用jQuery):

$(document).ready(function() { 
    $('.like').click(function() { 
     var id = $(this).attr('comment-id'); 
     $.ajax({ 
      url: 'add_like.php', 
      method: 'POST', 
      data: {'id':id}, 
      success: function(res) { 
       if (res === '1') { 
        var likes = $('.like[comment-id='+id+']').find('.num_likes').text(); 
        likes++; 
        var p = likes > 1 ? 'people like' : 'person likes'; 
        var html = '<span class="num_likes">'+likes+'</span> '+p+' this'; 
        $('.like[comment-id='+id+']').find('likes').html(html); 
       } 
       else alert('Error Liking Comment'); 
      }, 
      error: function() { alert('Error liking comment'); } 
     }); 
    }); 
}); 

而PHP(用于AJAX) - add_like.php

<?php 

if (!isset($_POST['id'])) exit; 
$id = $_POST['id']; 
// connect/select db 
$res = mysql_query('INSERT INTO `comment_likes` WHERE `comment_id` = '.$id.';'); 
if ($res) { 
    die(1); // success 
} 
die(0); 

?> 

显然,你会想扩大这是喜欢/不喜欢 - 因为目前用户可以喜欢多次,但你明白了。

+0

这是我的问题,它喜欢错了评论,它张贴喜欢的权数为每个评论,但你不能喜欢它。 – 2011-12-20 04:16:28

+0

我以前曾问过这个问题,但没有得到任何回复,你可以通过“当我点击我喜欢的顶级评论而不是我想要的那个按钮时点击我的问题代码”。谢谢你的帮助,并请不要停止(这就是其他人所做的) – 2011-12-20 04:18:55

+0

@AustinWheeler看到更新的答案 – 2011-12-20 14:40:38