{% for i in coupontesting %}
        <center>

            <div class="rectangle">
        <span class="right">Store Link</span><span class="left">{{ i.seller_store }}</span>
        <div class="coupon-frame">
                            <div class="coupon-left-div coupon-align-center">
                                <div style="padding: 1.125rem;border-left: 1px solid #d4d4d4;">
                                    <div style="position:relative;">
                                        <div class="coupon-left-img-div text-center coupon-align-center orange pt-32">
                        <span class="bold-18">{{ i.name }}</span>
                                        </div>
                                    </div>
                                </div>
                                <div class="coupon-ticket-frame">
                                    <div class="coupon-ticket-frame-style">
                                        <div class="oupon-ticket-frame-line"></div>
                                    </div>
                                </div>
                            </div>
                            <div class="coupon-right-div coupon-align-center">
                    {{ i.coupon_code }}
                            </div>

                <div class="coupon-right-div coupon-align-center">
                    <button> Use Now </button>
                </div>
        </div>
        <br><br>
        </div>
        </center>
    {% endfor %}

Above is the view page

Below is my query from Model Page

   $query = $this->db->query("SELECT * FROM coupon c INNER JOIN coupon_customer cc ON c.coupon_id = cc.coupon_id LEFT JOIN coupon_store cs ON c.coupon_id = cs.seller_store_id LEFT JOIN seller_store ss ON c.seller_store_id = ss.seller_store_id WHERE cc.customer_id = $customer_id AND c.date_end > NOW() ");

       if ($query->num_rows) {
            return $query->rows;
        } else {
            return 0;
        }

Table Structure

Table: coupon
 coupon_id   name   coupon_code  date_start   date_end  

Table: coupon_customer 
coupon_id    customer_id

Table: coupon_store
 coupon_store_id    coupon_id    seller_store_id

Table: seller_store
 seller_store_id    seller_store_name    seller_id

Everything works fine , but i wanted to ask is there anyway to "grouping the same seller store together ?
Image : https://prnt.sc/rnjbbf (Result from my code)

What i wanted: https://prnt.sc/rnjbqs

Any idea that how to do nested array ? have problem to change my result into nested array

I'm going to try formatting your query:

SELECT *
FROM coupon c
INNER JOIN coupon_customer cc ON c.coupon_id = cc.coupon_id
LEFT JOIN coupon_store cs ON c.coupon_id = cs.seller_store_id
LEFT JOIN seller_store ss ON c.seller_store_id = ss.seller_store_id
WHERE
    cc.customer_id = $customer_id AND
    c.date_end > NOW() 

You can use the GROUP_BY clause to aggregate data. For example, tabular data such as:

Store A     5 coupons

Store B 3 coupons

However, MySQL is always going to return data in tabular format: rows and columns. How do you want it to group rows from the stores together?

You can do something like this (untested code), assuming there are multiple coupon IDs for each coupon store:

SELECT
    coupon_store.coupon_store_id,
    GROUP_CONCAT(coupon.coupon_id)
FROM coupon
INNER JOIN coupon_store ON (coupon_store.id = coupon.id)
GROUP BY coupon_store.coupon_store_id

That should give you a comma-delimited list of coupons available for each store.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.