簡體   English   中英

從codignitor中的另一個mysql表獲取基於post_id匹配的會話user_id結果

[英]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 JOIN postsposts_meta post_id = posts post_id WHERE status = '1' AND user_id = '118' GROUP BY network_name ORDER BY network_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM