如何获得我需要的变量值

时间:2011-12-20 01:09:20

标签: php mysql forms list variables

如何获取我需要的变量值,我已经通过MySQL选择了注释和类似的值并创建了一个类似的按钮,但是当我点击时,它会将类似内容添加到最后选择/发布的变量值,所以它喜欢最后生成的评论。如何让它像我想要的那样,而不是最后选择的?

示例:

我有id的(1,2,3)注释,当它生成时按顺序(1,2,3)命令它们当我喜欢id = 2的注释它喜欢注释3因为它是最后生成的所以变量我喜欢评论2时,id的值为3。

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

2 个答案:

答案 0 :(得分: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);

?>

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

答案 1 :(得分: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

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

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!';
}
}

?>