نقل قول: مشکل در ترکیب ارایه ها
اومدم بدین صورت نوشتم و به اون حالتی که میخواستم رسیدم :
$sql="SELECT post_id_FK FROM tbl_comments GROUP BY post_id_FK"; $result = $this->connect()->prepare($sql);
$result->execute();
$count = $result->rowCount();
$sql="SELECT * FROM tbl_posts";
$result = $this->connect()->prepare($sql);
$result->execute();
$posts= $result->fetchAll(\PDO::FETCH_ASSOC);
for($post_id=1; $post_id <= $count; $post_id++){
$sql="SELECT * FROM tbl_comments WHERE post_id_FK=? ";
$result = $this->connect()->prepare($sql);
$result->bindValue(1, $post_id);
$result->execute();
$posts[$post_id-1]['comment']= $result->fetchAll(\PDO::FETCH_ASSOC) ;
}
return $posts;
اما دراین حالت مجبورشدم با حلقه چندبار به دیتابیس وصل بشم تا به اون حالت اطلاعات استخراج شوند.
به نظرتون راه دیگری وجود نداشت ؟ خود sql کوئری نداشت که به اون صورت گروه بندی کنه ؟!
نقل قول: مشکل در ترکیب ارایه ها
همه پست و کامنت ها رو بخون یبار
بعد تو حلقه بزرا و به هم وصلشون کن نیاز نداری توی حلقه کوئری بنویسیی
$posts="SELECT * FROM tbl_posts";
$comments ="SELECT * FROM tbl_comments ";
foreach($posts as $k=>$post ){
$posts[$k]['comment'] = [];
foreach($comments as $comment )
{
if($comment['post_id_FK'] == $post['id'])
{
$posts[$k]['comment'][] = $comment ;
}
}
}