将数据集显示为单个帖子

I am trying to make a stories system like Facebook or Instagram. As you can see in the table below s_id is unique post id and uid_fk post created user id.

enter image description here

uid_fk => 4 created 3 post in this table.

and

uid_fk => 5 created also 3 post.

If the query is:

$query = mysqli_query($this->db,"SELECT s_id, uid_fk, stories_img FROM stories_posts") or die(mysqli_error($this->db));

then html output looks like this

<!--
    id=s_id , 
    data_uidfk=uid_fk ,
    div inside imgage stories_img
-->

<div class="post" id="1" data_uidfk="4">15019557244.jpg</div>
<div class="post" id="2" data_uidfk="5">150021986050.png</div>
<div class="post" id="3" data_uidfk="4">14939786704.gif</div>
<div class="post" id="4" data_uidfk="5">14939786777.jpg</div>

<!--And other posts-->

My question is how can I display dataset as a single post like this,

<!--
        id=s_id , 
        data_uidfk=uid_fk ,
        div inside imgage stories_img
    -->

    <div class="post" data_uidfk="4">
        All uid_fk = 4 posts will be show in this div. 
    </div>
    <div class="post" data_uidfk="5">
        All uid_fk = 5 posts will be show in this div.
    </div>

    <!--And other posts-->

Thank you in advance for your help.

You could change your query to:

SELECT uid_fk, group_concat(stories_img) as pics
FROM stories_posts
GROUP BY uid_fk

The records will be return as:

uid_fk   pics
4        pic1,pic2,pi6
5        pic4,pic5,pic7

Then explode to set the individual images and place them in the necessary html element.