将PHP Like和Different集成到codeigniter中

时间:2017-07-19 22:13:03

标签: php codeigniter

我尝试在codeigniter中创建一个Like和不同的按钮,当点击它时,它会添加一个。该按钮将保存在mySql posts表的数据库中(posts表包含id,body,date,like,post_image),每次点击按钮时,在数据库中通过向其添加1来更新like或不同的字段。我试过想过这个但是我很困惑。还有一个postlike表包含(id,post_id,time,ipaddress)。

模型:Post_model

 public function get_data(){
    $query=$this->db->query("SELECT *
                             FROM posts 
                             ORDER BY st.id ASC");
    return $query->result_array();
}

CONTROLLER :帖子

 public function contents(){
    $this->data['get_data']= $this->post_model->get_data();
    $this->load->view('contents', $this->data, FALSE);
}

public function savelikes(){
$ipaddress=$_SERVER['REMOTE_ADDR'];
$post_id=$this->input->post('post_id');
$fetchlikes=$this->db->query('select likes from posts where id="'.$post_id.'"');
$result=$fetchlikes->result();
$checklikes = $this->db->query('select * from postlikes 
                                where post_id="'.$post_id.'" 
                                and ipaddress = "'.$ipaddress.'"');
$resultchecklikes = $checklikes->num_rows();

if($resultchecklikes == '0' ){
if($result[0]->likes=="" || $result[0]->likes=="NULL")
{
    $this->db->query('update posts set likes=1 where id="'.$post_id.'"');
}
else
{
    $this->db->query('update posts set likes=likes+1 where id="'.$post_id.'"');
}
$data=array('post_id'=>$post_id,'ipaddress'=>$ipaddress);
$this->db->insert('postlikes',$data);
}else{
$this->db->delete('postlikes', array('post_id'=>$post_id,
                                      'ipaddress'=>$ipaddress));
$this->db->query('update posts set likes=likes-1 where id="'.$post_id.'"');
}
$this->db->select('likes');
$this->db->from('posts');
$this->db->where('id',$post_id);
$query=$this->db->get();
$result=$query->result();
echo $result[0]->likes;
}

查看

<?php foreach ($posts as $post) : ?> 
<div class=" row">
<div class="  col s12 m4 l8" id="card">
      <img src="<?php echo site_url() ?>assets/images/posts/<?php echo        $post['post_image']; ?>">
      <a class="btn-floating halfway-fab waves-effect waves-light red" href="<?php echo site_url('/posts/'. $post['slug']); ?>"><i class="material-icons">add</i></a>
    </div>
    <div class="card-content">
        <span class=" center card-title"><?php echo $post['title']; ?></span>
        <small class=" center post-date"> Posted on: <?php echo $post['created_at']; ?> in <strong><?php echo $post['name']; ?></strong></small><br>
<!--THIS IS THE DOT DOT DOT THAT TRUNCATES THE  POST LENGTH-->
<p>  <?php echo word_limiter($post['body'], 40) ; ?></p><br><br>
   <?php
  if(isset($get_data) && is_array($get_data) && count($get_data));$i=1;
    foreach ($get_data as $key => $data) { ?>
<p><a onclick="javascript:savelike(<?php echo $data['id'];?>);">
 <i class="fa fa-thumbs-up"></i> 
 <span id="like_<?php echo $data['id'];?>">
    <?php if($data['likes']>0){echo $data['likes'].' Likes';}else{echo 'Like';} ?>
 </span></a>
</p>    
 <?php } endif; ?>
</div>
  </div>
     <?php endforeach; ?>

AJAX

 function savelike(post_id){
    $.ajax({
            type: "POST",
            url: "<?php echo site_url('Posts/savelikes');?>",
            data: "Post_id="+post_id,
            success: function (response) {
             $("#like_"+post_id).html(response+" Likes");

            }
        });
 }

1 个答案:

答案 0 :(得分:0)

现在,只是从Like(小例子)开始:

  1. 用户表 CREATE TABLE usersuid int NOT NULL PRIMARY KEY AUTO_INCREMENT, username varchar(25)NOT NULL UNIQUE, password varchar(50)NOT NULL, email varchar(100)NOT NULL );
  2. 消息表 CREATE TABLE messagesmsg_id int(11)NOT NULL PRIMARY KEY AUTO_INCREMENT, message varchar(200)NOT NULL, uid_fk int(11)NOT NULL, like_count int(11)DEFAULT NULL, created int(11)DEFAULT NULL, FOREIGN KEY(uid_fk)REFERENCES用户(uid) );
  3. 像表一样的消息 CREATE TABLE message_likelike_id int(11)NOT NULL PRIMARY KEY AUTO_INCREMENT, msg_id_fk int(11), uid_fk int(11)NOT NULL, created int(11)NOT NULL, FOREIGN KEY(uid_fk)REFERENCES用户(uid), FOREIGN KEY(msg_id_fk)REFERENCES消息(msg_id) );