查询结果并显示在网页上(嵌套数组)

Query result and display on a webpage (Nested Array)

    {% for i in coupontesting %}
    <center>

        <div class="rectangle">
    <span class="right">Store Link</span><span class="left">{{ i.seller_store_name   }}</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 %}

以上为查看页

以下是我在模型页面中的查询

   $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 结构

Table: coupon
 coupon_id(PK)   name   coupon_code  date_start   date_end  



Table: coupon_customer 
coupon_id(FK)    customer_id(FK)



Table: coupon_store
 coupon_store_id(PK)    coupon_id(FK)    seller_store_id(FK)



Table: seller_store
 seller_store_id(PK)    seller_store_name    seller_id(FK)

 Table: seller
 seller_id(FK)     seller_name   seller_email

 Table: customer
 customer_id(PK)    customer_name customer_email  

一切正常,但我想问一下是否可以“将同一卖家商店组合在一起? 图片:https://prnt.sc/rnjbbf(我的代码的结果)

我想要的:https://prnt.sc/rnjbqs

如果是同一家商店,那么它将组合在一起,而不是换行再次显示商店名称

假设这样的查询结果集(为简洁起见,我删除了大部分字段):

$rows = [
    ['seller_store_id' => 1, 'seller_store_name' => 'Foodlama', 'seller_id' => 11, 'coupon_id' => 6322],
    ['seller_store_id' => 2, 'seller_store_name' => 'BlueFood Market', 'seller_id' => 33, 'coupon_id' => 555],
    ['seller_store_id' => 2, 'seller_store_name' => 'BlueFood Market', 'seller_id' => 33, 'coupon_id' => 7787],
];

然后您可以重新格式化该结果集以按商店分组:

$reformatted = [];
foreach ($rows as $row) {
    // here we get all the other key => value pairs that aren't used for grouping
    $nonStoreInfo = array_filter($row, function ($key) {
        return $key !== 'seller_store_id' && $key !== 'seller_store_name' && $key !== 'seller_id';
    }, ARRAY_FILTER_USE_KEY);
    /*
     * We have to manually add any data that is common for the group.
     *
     * Here we overwrite it with each iteration to avoid unnecessary conditional statements
     * (checking if the key exists and has a value). It's cleaner and more concise like this.
     * It doesn't matter because it is the same for every group anyway.
    */
    $reformatted[$row['seller_store_id']]['seller_store_name'] = $row['seller_store_name'];
    $reformatted[$row['seller_store_id']]['seller_id'] = $row['seller_id'];
    $reformatted[$row['seller_store_id']]['coupons'][] = $nonStoreInfo;
}

请注意,我猜测了 seller_id。如果任何 seller_store_id 都可以不同,那么您必须将其从 array_filter 回调中删除,并删除手动分配 $reformatted[$row['seller_store_id']]['seller_id'] = $row['seller_id'];。同样,如果您需要 添加 某些内容到组中,则必须在 array_filter 中添加键比较并添加手动分配。

这最终会输出这样一个数组:

Array (
    [1] => Array (
        [seller_store_name] => Foodlama
        [seller_id] => 11
        [coupons] => Array (
            [0] => Array (
                [coupon_id] => 6322
            )
        )
    )
    [2] => Array (
        [seller_store_name] => BlueFood Market
        [seller_id] => 33
        [coupons] => Array (
            [0] => Array (
                [coupon_id] => 555
            )
            [1] => Array (
                [coupon_id] => 7787
            )
        )
    )
)

现在在您的模板中,您可以迭代顶级(商店),然后迭代商店内的优惠券。您可以在组中使用数组结构和您需要的确切数据来满足您的需要。