[英]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.