我是靠谱客的博主 想人陪金毛,最近开发中收集的这篇文章主要介绍php后台关于根据某个相同的日期进行分组,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

接口中发现需要需要将每天(及时Unix时间不同),因为unix时间不同,需要将这个时间转成年月日这样,然后作为条件来查询这一天直播的历史数据。然后去这一天所有的数据,技术遇到的问题是,如何将这一天的数据放在一起,用sql分组不可以,因为uninx时间是不同的。所以我采用了函数循环比例进行分组。
**没进行分组的代码**
public function merchants_dynamic()
{
if ($params = Request::instance()->isPost()) {
$p = empty($params["p"]) ? 1 :$params["p"];
$pageSize = empty($params["pagesize"]) ? 20 : $params["pagesize"];
$merchants_id = input('merchants_id');//商家商户id
if (!$merchants_id) error("商户店铺id不能为空");
$list = Db::name('live_store')->alias("a")
->field("a.live_store_id,d.share,d.watch_nums,d.end_time,b.title,b.apply_id,b.cover_img,b.class_id,c.business_img,c.company_name")
->join("apply b",'a.room_id=b.room_id')
->join("merchants c","b.merchants_id=c.gl_merchants_id")
->join("live d","d.live_id=a.live_id")
->where(['c.gl_merchants_id'=>$merchants_id])
->limit(($p-1)*$pageSize,$pageSize)
->select();
foreach ($list as $k=>$v){
$list[$k]['tag']=Db::name('live_class')->where(['live_class_id'=>$v['class_id']])->value('tag');
$list[$k]['end_time'] = date("Y-m-d",$v['end_time']);
}
if($list){
$data = $list;
}else{
$data = [];
}
return success($data);
}
}
**这里没分组的结果**
{
"status": "ok",
"data": [
{
"live_store_id": "1",
"share": "0",
"watch_nums": "0",
"end_time": "2018-04-12",
"title": "测试开发",
"apply_id": "35",
"cover_img": "https://file2.kaopuke.com:8081/files_image/20230403/82201725481.jpg&;,
"class_id": "6",
"business_img": "https://file2.kaopuke.com:8081/files_image/20230403/82201552182.jpg&;,
"company_name": "金帅集团2",
"tag": "直播售卖"
},
{
"live_store_id": "2",
"share": "0",
"watch_nums": "0",
"end_time": "2018-04-13",
"title": "测试开发",
"apply_id": "35",
"cover_img": "https://file2.kaopuke.com:8081/files_image/20230403/82201725481.jpg&;,
"class_id": "6",
"business_img": "https://file2.kaopuke.com:8081/files_image/20230403/82201552182.jpg&;,
"company_name": "金帅集团2",
"tag": "直播售卖"
},
{
"live_store_id": "1",
"share": "0",
"watch_nums": "0",
"end_time": "2018-04-12",
"title": "测试开发",
"apply_id": "35",
"cover_img": "https://file2.kaopuke.com:8081/files_image/20230403/82201725481.jpg&;,
"class_id": "6",
"business_img": "https://file2.kaopuke.com:8081/files_image/20230403/82201552182.jpg&;,
"company_name": "王氏帝国2",
"tag": "直播售卖"
},
{
"live_store_id": "2",
"share": "0",
"watch_nums": "0",
"end_time": "2018-04-13",
"title": "测试开发",
"apply_id": "35",
"cover_img": "https://file2.kaopuke.com:8081/files_image/20230403/82201725481.jpg&;,
"class_id": "6",
"business_img": "https://file2.kaopuke.com:8081/files_image/20230403/82201552182.jpg&;,
"company_name": "王氏帝国2",
"tag": "直播售卖"
}
]
}

这里按照end_time进行分组,将同一天的数据分组出来代码段

public function merchants_dynamic()
{
if ($params = Request::instance()->isPost()) {
$p = empty($params["p"]) ? 1 :$params["p"];
$pageSize = empty($params["pagesize"]) ? 20 : $params["pagesize"];
$merchants_id = input('merchants_id');//商家商户id
if (!$merchants_id) error("商户店铺id不能为空");
$list = Db::name('live_store')->alias("a")
->field("a.live_store_id,d.share,d.watch_nums,d.end_time,b.title,b.apply_id,b.cover_img,b.class_id,c.business_img,c.company_name")
->join("apply b",'a.room_id=b.room_id')
->join("merchants c","b.merchants_id=c.gl_merchants_id")
->join("live d","d.live_id=a.live_id")
->where(['c.gl_merchants_id'=>$merchants_id])
->limit(($p-1)*$pageSize,$pageSize)
->select();
foreach ($list as $k=>$v){
$list[$k]['tag']=Db::name('live_class')->where(['live_class_id'=>$v['class_id']])->value('tag');
$list[$k]['end_time'] = date("Y-m-d",$v['end_time']);
}
$res = array();
foreach ($list as $key=>$val){
$res[$val['end_time']][] = $val;
}
$re = [];
foreach ($res as $ke=>$va){
$re[]['time'] = $ke;
foreach ($re as $a=>$v){
foreach ($va as $key=>$value){
if($v['time']==$value['end_time']){
$re[$a]['list'] = $va;
}
}
}
}
if($list){
$data = $re;
}else{
$data = [];
}
return success($data);
}
}

这里是结果

{
"status": "ok",
"data": [
{
"time": "2018-06-22",
"list": [
{
"live_store_id": "1",
"intime": "1529660340",
"end_time": "2018-06-22",
"title": "画展展销现场-专场-0622-002",
"apply_id": "113",
"cover_img": "https://file2.kaopuke.com:8081/files_image/20230403/82201196719.jpeg&;,
"class_id": "0",
"status": "1",
"business_img": "https://file2.kaopuke.com:8081/files_image/20230403/822013023410.jpg&;,
"is_shenhe": "6",
"company_name": "上海龙举资产",
"live_id": "3",
"watch_nums": 0,
"tag": "展销直播",
"share_nums": "2",
"type": "3"
},
{
"live_store_id": "2",
"intime": "1529663681",
"end_time": "2018-06-22",
"title": "画展售卖-专场-0622-001",
"apply_id": "112",
"cover_img": "https://file2.kaopuke.com:8081/files_image/20230403/822016687411.jpeg&;,
"class_id": "0",
"status": "2",
"business_img": "https://file2.kaopuke.com:8081/files_image/20230403/822013023410.jpg&;,
"is_shenhe": "6",
"company_name": "上海龙举资产",
"live_id": "1",
"watch_nums": 0,
"tag": "直播售卖",
"share_nums": "1",
"type": "3"
}
]
},
{
"time": "2018-06-23",
"list": [
{
"live_store_id": "3",
"intime": "1529737079",
"end_time": "2018-06-23",
"title": "油画拍卖专场-0622-004",
"apply_id": "115",
"cover_img": "https://file2.kaopuke.com:8081/files_image/20230403/822017791913.jpeg&;,
"class_id": "0",
"status": "3",
"business_img": "https://file2.kaopuke.com:8081/files_image/20230403/822013023410.jpg&;,
"is_shenhe": "6",
"company_name": "上海龙举资产",
"live_id": "4",
"watch_nums": 0,
"tag": "直播拍卖",
"share_nums": "0",
"type": "3"
}
]
},
{
"time": "2018-06-25",
"list": [
{
"live_store_id": "5",
"intime": "1529913913",
"end_time": "2018-06-25",
"title": "展销现场-0622-005",
"apply_id": "116",
"cover_img": "https://file2.kaopuke.com:8081/files_image/20230403/822018887315.jpeg&;,
"class_id": "0",
"status": "1",
"business_img": "https://file2.kaopuke.com:8081/files_image/20230403/822013023410.jpg&;,
"is_shenhe": "6",
"company_name": "上海龙举资产",
"live_id": "5",
"watch_nums": 0,
"tag": "展销直播",
"share_nums": "0",
"type": "3"
},
{
"live_store_id": "6",
"intime": "1529920290",
"end_time": "2018-06-25",
"title": "拍卖直播-专场-0622-006",
"apply_id": "117",
"cover_img": "https://file2.kaopuke.com:8081/files_image/20230403/822012954017.jpeg&;,
"class_id": "0",
"status": "3",
"business_img": "https://file2.kaopuke.com:8081/files_image/20230403/822013023410.jpg&;,
"is_shenhe": "6",
"company_name": "上海龙举资产",
"live_id": "6",
"watch_nums": 0,
"tag": "直播拍卖",
"share_nums": "0",
"type": "3"
}
]
},
{
"time": "2018-06-26",
"list": [
{
"live_store_id": "8",
"intime": "1530006248",
"end_time": "2018-06-26",
"title": "业业",
"apply_id": "125",
"cover_img": "https://file2.kaopuke.com:8081/files_image/20230403/822011934519.png&;,
"class_id": "0",
"status": "3",
"business_img": "https://file2.kaopuke.com:8081/files_image/20230403/822013023410.jpg&;,
"is_shenhe": "6",
"company_name": "上海龙举资产",
"live_id": "8",
"watch_nums": 0,
"tag": "直播拍卖",
"share_nums": "0",
"type": "3"
},
{
"live_store_id": "11",
"intime": "1530009058",
"end_time": "2018-06-26",
"title": "cs108",
"apply_id": "126",
"cover_img": "https://file2.kaopuke.com:8081/files_image/20230403/822017071221.jpeg&;,
"class_id": "0",
"status": "2",
"business_img": "https://file2.kaopuke.com:8081/files_image/20230403/822013023410.jpg&;,
"is_shenhe": "6",
"company_name": "上海龙举资产",
"live_id": "14",
"watch_nums": 0,
"tag": "直播售卖",
"share_nums": "0",
"type": "3"
},
{
"live_store_id": "12",
"intime": "1530009560",
"end_time": "2018-06-26",
"title": "ceshi901",
"apply_id": "127",
"cover_img": "https://file2.kaopuke.com:8081/files_image/20230403/822018673723.jpeg&;,
"class_id": "0",
"status": "2",
"business_img": "https://file2.kaopuke.com:8081/files_image/20230403/822013023410.jpg&;,
"is_shenhe": "6",
"company_name": "上海龙举资产",
"live_id": "15",
"watch_nums": 0,
"tag": "直播售卖",
"share_nums": "0",
"type": "3"
}
]
}
]
}

END

最后

以上就是想人陪金毛为你收集整理的php后台关于根据某个相同的日期进行分组的全部内容,希望文章能够帮你解决php后台关于根据某个相同的日期进行分组所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部