Codeigniter 从另一个 table For Post List Array for API response 获取 Comments Count
Code Igniter Get Comments Count from another table For Post List Array for API response
您好,
我正在 Code Igniter 中处理 Posts 和评论模型响应 API!
我的控制器:
public function getPosts()
{
if (isset($_POST["getPosts"]))
{
$data = $this->api_model->getPosts();
$json_response2 = array('status' => 'success', 'postList' => $data->result_array());
echo json_encode($json_response2);
}
else
{
$data['status'] = 'error';
echo json_encode($data);
}
}
我的模特:
public function get_posts()
{
$this->db->order_by('postID', 'DESC');
$query = $this->db->get('posts');
return $query->result_array();
}
MYSQL 表:
for Posts---
postID | postTitle | postBody
for Comments---
commentID | postID | commentBody
我想在 API 中获取 post 数组的评论数,以响应 post 的显示列表和 Post 的评论数?
谢谢!
您需要加入您的 评论 table 才能按您的帖子 ID 进行计数和分组。
按如下方式修改模型中的 get_posts()
函数:
$this->db->from('posts');
$this->db->select("posts.*, count(*) as comments_count");
$this->db->join('comments', 'posts.postID = comments.postID');
$this->db->group_by('posts.postID');
$this->db->order_by('postID', 'DESC');
return $this->db->get()->result_array();
此外,您使用了 result_array() 两次,这是错误的。更改您的控制器线路
$json_response2 = array('status' => 'success', 'postList' => $data->result_array());
作为
$json_response2 = array('status' => 'success', 'postList' => $data);
因为您已经从模型中获得结果。
此外,在控制器中使用正确的功能名称,$this->api_model->get_posts();
希望对您有所帮助。
这是您在单个查询中获取所有这些数据所需的查询。它将用左连接子句连接 table 并且还会给你评论数。
SELECT
p.`postID`,
`postTitle`,
`postBody`,
COUNT(c.commentID) AS comment_cnt
FROM
`Posts` AS p
LEFT JOIN `Comments` AS c
ON p.postID = c.postID
GROUP BY c.postID
ORDER BY p.postID DESC ;
N:B left join is mandatory, otherwise it will not give you zero
commented post.
将其转换为 codeigniter,您可以将其编写为。
$this->db->from('Posts p');
$this->db->join('Comments c','p.postID = c.postID','left');
$this->db->group_by('c.postID');
$this->db->order_by('p.postID', 'DESC');
$this->db->select('p.*,COUNT(c.commentID) AS comment_cnt');
$query = $this->db->get();
$res = $query->result_array();
希望对您有所帮助。
您好,
我正在 Code Igniter 中处理 Posts 和评论模型响应 API!
我的控制器:
public function getPosts()
{
if (isset($_POST["getPosts"]))
{
$data = $this->api_model->getPosts();
$json_response2 = array('status' => 'success', 'postList' => $data->result_array());
echo json_encode($json_response2);
}
else
{
$data['status'] = 'error';
echo json_encode($data);
}
}
我的模特:
public function get_posts()
{
$this->db->order_by('postID', 'DESC');
$query = $this->db->get('posts');
return $query->result_array();
}
MYSQL 表:
for Posts---
postID | postTitle | postBody
for Comments---
commentID | postID | commentBody
我想在 API 中获取 post 数组的评论数,以响应 post 的显示列表和 Post 的评论数?
谢谢!
您需要加入您的 评论 table 才能按您的帖子 ID 进行计数和分组。
按如下方式修改模型中的 get_posts()
函数:
$this->db->from('posts');
$this->db->select("posts.*, count(*) as comments_count");
$this->db->join('comments', 'posts.postID = comments.postID');
$this->db->group_by('posts.postID');
$this->db->order_by('postID', 'DESC');
return $this->db->get()->result_array();
此外,您使用了 result_array() 两次,这是错误的。更改您的控制器线路
$json_response2 = array('status' => 'success', 'postList' => $data->result_array());
作为
$json_response2 = array('status' => 'success', 'postList' => $data);
因为您已经从模型中获得结果。
此外,在控制器中使用正确的功能名称,$this->api_model->get_posts();
希望对您有所帮助。
这是您在单个查询中获取所有这些数据所需的查询。它将用左连接子句连接 table 并且还会给你评论数。
SELECT
p.`postID`,
`postTitle`,
`postBody`,
COUNT(c.commentID) AS comment_cnt
FROM
`Posts` AS p
LEFT JOIN `Comments` AS c
ON p.postID = c.postID
GROUP BY c.postID
ORDER BY p.postID DESC ;
N:B left join is mandatory, otherwise it will not give you zero commented post.
将其转换为 codeigniter,您可以将其编写为。
$this->db->from('Posts p');
$this->db->join('Comments c','p.postID = c.postID','left');
$this->db->group_by('c.postID');
$this->db->order_by('p.postID', 'DESC');
$this->db->select('p.*,COUNT(c.commentID) AS comment_cnt');
$query = $this->db->get();
$res = $query->result_array();
希望对您有所帮助。