[英]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 %}
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(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
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)一切正常,但我想问一下是否有“将同一个卖家商店分组在一起?图片: https : //prnt.sc/rnjbbf (来自我的代码的结果)
What i wanted: https://prnt.sc/rnjbqs我想要的: https : //prnt.sc/rnjbqs
If same store then it would group together instead of new line showing the store name again如果是同一家商店,那么它将组合在一起,而不是再次显示商店名称的新行
Assuming a query result set like this (I removed most of the fields for brevity):假设这样的查询结果集(为简洁起见,我删除了大部分字段):
$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],
];
You can then reformat that result set to be grouped by the store:然后,您可以重新格式化该结果集以按商店分组:
$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;
}
Note that I made a guess about seller_id
.请注意,我对
seller_id
进行了猜测。 If it can be different for any seller_store_id
, then you have to remove it from the array_filter
callback and also remove the manual assignment $reformatted[$row['seller_store_id']]['seller_id'] = $row['seller_id'];
如果任何
seller_store_id
可能不同,那么您必须将其从array_filter
回调中删除,并删除手动分配$reformatted[$row['seller_store_id']]['seller_id'] = $row['seller_id'];
. . Likewise, if you need to add something to the group, you'd have to add the key comparison in
array_filter
and add the manual assignment.同样,如果您需要向组中添加某些内容,则必须在
array_filter
添加键比较并添加手动分配。
This will ultimately output an array like this:这将最终输出一个这样的数组:
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
)
)
)
)
Now in your template you can iterate the top level (stores) and then iterate the coupons within the stores.现在在您的模板中,您可以迭代顶层(商店),然后在商店内迭代优惠券。 You can play around with the array structure and the exact data you need in the group to fit your needs.
您可以使用数组结构和组中所需的确切数据来满足您的需要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.