I have 2 different tables: reputation and posts. POSTS have details related to it like post_id, user_id, post_content... and so on. REPUTATION have details like post_id and user_id. If a pair exists in the table then the post_id has been +1'ed by the user_id.
On my homepage, I'm using pagination to display 5 posts/page and only fetching from the POSTS table. Further I'm trying to fetch the 'post_id' from REPUTATION table for the 'user_id' in $_SESSION.
public function index()
{
$this->load->model('themodel');
$this->load->library('pagination');
$config['base_url'] = site_url('trial/index');
$config['total_rows'] = $this->themodel->total_rows('posts');
$config['per_page'] = 5;
//$config['display_pages'] = FALSE;
$this->pagination->initialize($config);
$offset = $this->uri->segment(3);
$data['details'] = $this->themodel->list_posts($config['per_page'], $offset);
$data['links'] = $this->pagination->create_links();
//Check for session and load the reputation data
if($this->session->userdata('loggedIn') && $this->session->userdata('user'))
{
//fetch reputation by user
$data['repbyuser'] = $this->themodel->getrepbyuser();
}
$this->load->view('home_view', $data);
}
In the model part:
public function list_posts($limit, $start)
{
$this->db->select('post_id, user_id, post_title, post_content, total_reputation, post_time, total_reviews');
return $this->db->get('posts', $limit, $start)->result_array();
}
public function getrepbyuser()
{
$this->db->select('post_id');
$this->db->where('user_id', $this->session->userdata('user'));
$result = $this->db->get('reputation');
if($result->num_rows() > 0)
return $result->result_array();
}
Now on my homepage I'm traversing the $details
array but I'm not sure how to match the results from both the table.
If I'm doing anything wrong please guide. Any suggestion would be appreciated.
function getrepbyuser()
{
$data = array();
$this->db->select('post_id');
$this->db->where('user_id', $this->session->userdata('user'));
$result = $this->db->get('reputation');
if($result->num_rows() > 0){
//return $result->result_array();
$temp = $result->result_array();
foreach( $temp as $each ){ #for returning a single dimentional array
$data[] = $each['post_id'];
}
}
return $data;
}
Now in the view page you will do:
foreach( $details as $each ){ #loop for the posts
$liked = false;
if( in_array($key['post_id'], $repbyuser) ){ #check if the post is liked or not
$liked = true;
}
if( $liked ){
#button for dislike
}else{
#button for like
}
}