[英]get session user_id result based on post_id match from another mysql table in codignitor
我创建了一个函数来计算基于以下2个数据库表的帖子总数
1个帖子:
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
由于usersid存在于posts表中,而两个表上的唯一标识符是post_id,我正在计算状态= 1和user_id = $ user_id的所有帖子, 这是到目前为止我的模型函数
/**
* 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;
}
}
这是到目前为止我的控制器功能
// 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();
发生数据库错误,错误号:1052
where子句中的“状态”列不明确
选择network_name,将COUNT(meta_id)作为号码FROM
posts_meta
LEFT JOINposts
在posts_meta
。post_id
=posts
。post_id
WHEREstatus
= '1' ANDuser_id
= '118' GROUP BYnetwork_name
ORDER BYnetwork_name
文件名:models / Posts.php
行号:393
试试这个=>
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.