Jamez Roz
Jamez Roz

Reputation: 61

get session user_id result based on post_id match from another mysql table in codignitor

I created a function to count total number of posts that based on 2 db tables below

1- posts:

    post_id user_id 
=========== ==== 
          61 122
          62 122
          96 122
          97 122
          98 122

2- post_meta:

   meta_id  post_id   status 
   ======== =======   ======
       1      61         1
       2      62         0
       3      62         1
       4      91         1
       5      97         1
       6      98         1

since user_id is exist in posts table whereas unique identifier on both table is post_id i am looking to count all post where status = 1 and user_id = $user_id here is my model function so far

/**
 * The function get_all_user_post_count gets all count posts for admin dashboard
 * 
 * @return array with posts or false 
 */
public function get_all_user_post_count($user_id) {
    $this->db->select('network_name,COUNT(meta_id) as number', false);
    $this->db->from('posts_meta');
    $this->db->join('posts', 'posts_meta.post_id=posts.post_id', 'left');
    $this->db->where(['status' => '1', 'user_id' => $user_id]);
    $this->db->group_by('network_name');
    $this->db->order_by('network_name');
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        $result = $query->result();
        // Create new array
        $new_array = [];
        foreach ($result as $data) {
            $new_array[$data->network_name] = $data->number;
        }
        return $new_array;
    } else {
        return false;
    }
}

here is my controller function so far

// Count all sent posts per social network
        $count_sent_posts = $this->posts->get_all_user_post_count($this->user_id);
        $this->footer = [
            'statistics' => $statistics,
            'sent' => $count_sent_posts
        ];
        $this->user_layout();

A Database Error Occurred Error Number: 1052

Column 'status' in where clause is ambiguous

SELECT network_name, COUNT(meta_id) as number FROM posts_meta LEFT JOIN posts ON posts_meta.post_id=posts.post_id WHERE status = '1' AND user_id = '118' GROUP BY network_name ORDER BY network_name

Filename: models/Posts.php

Line Number: 393

Upvotes: 0

Views: 62

Answers (1)

Jasdeep Singh
Jasdeep Singh

Reputation: 36

Try this one =>

public function get_all_user_post_count($user_id) {
    $where = array('posts_meta.status ' => 1 , 'posts.user_id ' => $user_id);
    $this->db->select('posts_meta.network_name,COUNT(posts_meta.meta_id) as number', false);
    $this->db->from('posts_meta');
    $this->db->join('posts', 'posts_meta.post_id=posts.post_id', 'left');
    $this->db->where($where);
    $this->db->group_by('posts_meta.network_name');
    $this->db->order_by('posts_meta.network_name');
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        $result = $query->result();
        // Create new array
        $new_array = [];
        foreach ($result as $data) {
            $new_array[$data->network_name] = $data->number;
        }
        return $new_array;
    } else {
        return false;
    }
}

Upvotes: 2

Related Questions