如何使用jquery创建类似系统的帖子

时间:2016-08-06 10:42:25

标签: javascript php jquery ajax

function get_posts2()
{

    global $conn;
    $user=$_SESSION['user_email'];
    $get_user="SELECT * FROM users WHERE user_email='$user'";
    $run_user=mysqli_query($conn,$get_user);
    $row=mysqli_fetch_array($run_user);
    $user_id=$row['user_id'];
    $friendn=1;
   $friend_select="SELECT  * from friends where user_1_id='$user_id' AND friends_status='$friendn'";
    $run_select_friends=mysqli_query($conn,$friend_select);
    while ($row_friends=mysqli_fetch_array($run_select_friends)){
        $id_2=$row_friends['user_2_id'];

        $post_selcet="select * from posts WHERE user_id='$id_2'";
        $run_post_select=mysqli_query($conn,$post_selcet);

        while($row_posts=mysqli_fetch_array($run_post_select))
        {

            $post_id=$row_posts['post_id'];
            $u_id=$row_posts['user_id'];
            $post_title=$row_posts['post_title'];
            $content=$row_posts['post_content'];
            $post_date=$row_posts['post_date'];
            $post_image=$row_posts['post_image'];


            $user="select * from users where user_id='$u_id'";
            $run_user=mysqli_query($conn,$user);
            $row_user=mysqli_fetch_array($run_user);
            $user_name=$row_user['user_name'];
            $user_image=$row_user['user_image'];

            echo "
<div class='container'>
<div class='row'>
<div class='col-md-8'>
       
<div class='panel panel-default'>
  <div class='panel-heading' id='post_head'>
<div style='float: left'> <img src='user/user_image/$user_image' width='50' height='50'></div>
<div style='text-align: center'  id='post_title'> $post_title</div>

<div style='float: right'>
<div id='post_meta'>
 <a href='user_profile.php?u_id=$user_id' id='user_id'>$user_name</a>$post_date</div></div></div>
  <div class='panel-body'><p align='center'><img src=images/post_images/$post_image class='img-responsive'></p><p>$content</p>
  <p><a href='single.php?post_id=$post_id' style='float:right;'><span style='font-size: x-large' class='glyphicon glyphicon-comment'></span> </a>
  </p></div>
</div>       
       
       </div>
       </div></div>
       ";




        }








    }
    


}

我想创建像facebook或Instagram一样的帖子系统 任何人都可以帮助我吗?

我的代码显示我收到了我的帖子信息然后我用它们显示在我的页面中但我现在想要创建一个喜欢和不喜欢的系统

1 个答案:

答案 0 :(得分:1)

首先你必须在你的帖子表中创建“likeCount”或“dislikeCount”字段,然后提出增加或减少计数的ajax请求。

示例jQuery代码。

$(document).on('click','.vote',function(){
	
	var $this = $(this);
	var $postID = $this.data('post_id');
	var $action = $this.data('vote_type'); //example 'up' or 'down'
	
	$.ajax({

	url:'url/to/process/ajax',
	type:'POST',
	data:{
			action:$action,
			postID : $postID
		},
	dataType:'json',
	success:function(response){
		
		//do success action
		
		},
	error:function(){}	
});
	
	
});