dongsuo0517 2017-08-07 08:03
浏览 43

Codeigniter在关注用户后删除关注按钮

Hi I am able to display users friends on a page but when I into the friends profile I still see the follow button, is there anyway I can remove the button after following? I am using codeigniter. see code.

Model

public function get_friends($user_id = NULL){
    if ($user_id = $this->session->userdata('user_id')){
    $this->db->select('*');
    $this->db->from('users');
    $this->db->join('friends', 'friends.user_id = users.user_id');
    $this->db->where('friends.user_id2', $user_id);
    $query = $this->db->get();
    return $query->result_array($user_id);
        }
}

Controller

public function view($user_id = NULL){
    $data['friends'] = $this->Friend_model->get_friends($user_id);  
    $this->load->view('user/templates/header_view_subpage');
    $this->load->view('user/members_entry_view', $data);
    $this->load->view('user/templates/footer_view_subpage');

}

View

<?php
     $session = $this->session->userdata('user_id');
     $friends = $this->session->userdata('user_id2');
     if($session == $members['user_id']):
?> 
    <div class="col-md-6">
    <button type="button" class="btn btn-primary btn-sm btn-block"><i class="fa fa-envelope"></i> Send Message</button>
    </div>
<?php 
endif;
?>
  • 写回答

1条回答 默认 最新

  • drk7700 2017-08-07 08:10
    关注

    You'll have to write a function that checks if a user is already a friend or not, something like this:

    public function isFriend($id){
    return $this->Friend_model->is_friend($user_id);  
    }
    

    the is_friend method will fetch the database and tries to fetch a user with the $user_id, if there is a record then our function will return true otherwise it will return false;

    public function is_friend($user_id,$friend_id){ 
    $this->db->select('*'); 
    $this->db->from('friends');
    $this->db->where('user_id',$user_id); 
    $this->db->where('friend_id',$friend_id); 
    $query = $this->db->get(); 
    $results = $query->result_array(); 
    return count($results) ? 1 : 0; 
    } 
    

    After this we call the function in our view, if it returns true, then we don't show the button otherwise we show it.

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改