当我将id从模型传递给控制器时,它说数组不能转换为字符串..但我的问题是我想要所有帖子并根据帖子获取评论..我无法做到这一点
public function getPosts()
{
$sql="SELECT d.*,u.username,u.image FROM tbl_discussion as d join user as u
on d.user_id=u.user_id order by date desc";
$query=$this->db->query($sql);
return $query->result();
}
public function getReplies($post_id=NULL)
{
//$sql="SELECT r.*,u.username FROM tbl_replies as r
//join user as u on r.user_id=u.user_id
//join tbl_discussion as d on d.discussion_id=r.discussion_id where r.discussion_id='$post_id' order by date desc";
$sql="SELECT r.*,u.username FROM tbl_replies as r
join user as u on r.user_id=u.user_id
join tbl_discussion as d on d.discussion_id=r.discussion_id where r.discussion_id='$post_id'order by date desc";
$query=$this->db->query($sql);
if($query->num_rows()>0){
foreach ($query->result() as $data){
$row[]=$data;
}
return $row;
}
}
public function getId(){
$sql="select discussion_id from tbl_discussion";
$query= $this->db->query($sql);
return $query->result_array();
}
在控制器中
public function index()
{
$id=$this->kishan->getid();
$data1['agros']=$this->kishan->getNoticAgros();
$data2['farms']=$this->kishan->getNoticFarmers();
$data3['members']=$this->kishan->getNoticMembers();
$data4['ngoss']=$this->kishan->getNoticNgos();
$data5['followups']=$this->kishan->getNoticFollowUp();
$data6['all']=$this->kishan->getNoticAll();
$data7['posts']=$this->kishan->getPosts();
$data8['replies']=$this->kishan->getReplies($id);
$data9['page']='discussion';
$this->load->view('admin/home',array_merge($data1,$data2,$data3,$data4,$data5,$data6,$data7,$data8,$data9));
}
答案 0 :(得分:1)
按如下所示编写查询: -
$sql="SELECT r.*,u.*,d.* from tbl_replies as r
LEFT JOIN user as u ON r.user_id=u.user_id
LEFT JOIN tbl_discussion as d ON r.discussion_id = d.discussion_id";
希望它会对你有所帮助:)。
答案 1 :(得分:1)
添加注释中的WHERE语句。
$sql="SELECT * from tbl_replies as r
LEFT JOIN user as u ON r.user_id=u.user_id
LEFT JOIN tbl_discussion as d ON r.discussion_id = d.discussion_id
WHERE r.discussion_id='" . $post_id . "'";
我不确定我是否完全理解你的问题,但我们假设$ aPostIds是一个填充了所需id的数组:
// Escape all values
$aPostIdsEscaped=array()
foreach($aPostIds as $postId)
$aPostIdsEscaped[]=mysqli_real_escape_string($dblink, $postId);
// Turn array into IN clause
$sql="SELECT * from tbl_replies as r
LEFT JOIN user as u ON r.user_id=u.user_id
LEFT JOIN tbl_discussion as d ON r.discussion_id = d.discussion_id
WHERE r.discussion_id IN ('" . implode("', '", $aPostIdsEscaped) . "'";