一清二白

个人博客

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": "http://test.tstmobile.com/uploads//image/banner/20180412/37592d087ae79a103cea4417e77b6ddf.jpg",
            "class_id": "6",
            "business_img": "http://test.tstmobile.com/uploads/image/20171227/a7de424d12f4fdd77de7f6f9a35a8c48.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": "http://test.tstmobile.com/uploads//image/banner/20180412/37592d087ae79a103cea4417e77b6ddf.jpg",
            "class_id": "6",
            "business_img": "http://test.tstmobile.com/uploads/image/20171227/a7de424d12f4fdd77de7f6f9a35a8c48.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": "http://test.tstmobile.com/uploads//image/banner/20180412/37592d087ae79a103cea4417e77b6ddf.jpg",
            "class_id": "6",
            "business_img": "http://test.tstmobile.com/uploads/image/20171227/a7de424d12f4fdd77de7f6f9a35a8c48.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": "http://test.tstmobile.com/uploads//image/banner/20180412/37592d087ae79a103cea4417e77b6ddf.jpg",
            "class_id": "6",
            "business_img": "http://test.tstmobile.com/uploads/image/20171227/a7de424d12f4fdd77de7f6f9a35a8c48.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": "http://daylive.tstmobile.com/uploads//image/banner/20180622/47593f584f63015c7cb829da73627979.jpeg",
                    "class_id": "0",
                    "status": "1",
                    "business_img": "https://www.dev.ttzxh.com/imageRemote.php/shop/data/upload/media/5805f0c2ac68da90f85f4dfb138c9106/10267/43/image/20180605/1528175968296459.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": "http://daylive.tstmobile.com/uploads//image/banner/20180622/0b22719d91c4618adb480343a5dbd962.jpeg",
                    "class_id": "0",
                    "status": "2",
                    "business_img": "https://www.dev.ttzxh.com/imageRemote.php/shop/data/upload/media/5805f0c2ac68da90f85f4dfb138c9106/10267/43/image/20180605/1528175968296459.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": "http://daylive.tstmobile.com/uploads//image/banner/20180622/58e977b1d52a66446299fa82df5248bc.jpeg",
                    "class_id": "0",
                    "status": "3",
                    "business_img": "https://www.dev.ttzxh.com/imageRemote.php/shop/data/upload/media/5805f0c2ac68da90f85f4dfb138c9106/10267/43/image/20180605/1528175968296459.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": "http://daylive.tstmobile.com/uploads//image/banner/20180622/64927aa0408133dcebc986ab739da603.jpeg",
                    "class_id": "0",
                    "status": "1",
                    "business_img": "https://www.dev.ttzxh.com/imageRemote.php/shop/data/upload/media/5805f0c2ac68da90f85f4dfb138c9106/10267/43/image/20180605/1528175968296459.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": "http://daylive.tstmobile.com/uploads//image/banner/20180622/819762dbed46570a74cc067074cc94bf.jpeg",
                    "class_id": "0",
                    "status": "3",
                    "business_img": "https://www.dev.ttzxh.com/imageRemote.php/shop/data/upload/media/5805f0c2ac68da90f85f4dfb138c9106/10267/43/image/20180605/1528175968296459.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": "http://daylive.tstmobile.com/uploads/image/20171208/e2fabbfab76afa56660d831050c4c4d8.png",
                    "class_id": "0",
                    "status": "3",
                    "business_img": "https://www.dev.ttzxh.com/imageRemote.php/shop/data/upload/media/5805f0c2ac68da90f85f4dfb138c9106/10267/43/image/20180605/1528175968296459.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": "http://daylive.tstmobile.com/uploads//image/banner/20180626/78f3af8d9a24348a4ef30e61179bab1b.jpeg",
                    "class_id": "0",
                    "status": "2",
                    "business_img": "https://www.dev.ttzxh.com/imageRemote.php/shop/data/upload/media/5805f0c2ac68da90f85f4dfb138c9106/10267/43/image/20180605/1528175968296459.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": "http://daylive.tstmobile.com/uploads//image/banner/20180626/c2a8633793ec2bfb58410d1e056a295f.jpeg",
                    "class_id": "0",
                    "status": "2",
                    "business_img": "https://www.dev.ttzxh.com/imageRemote.php/shop/data/upload/media/5805f0c2ac68da90f85f4dfb138c9106/10267/43/image/20180605/1528175968296459.jpg",
                    "is_shenhe": "6",
                    "company_name": "上海龙举资产",
                    "live_id": "15",
                    "watch_nums": 0,
                    "tag": "直播售卖",
                    "share_nums": "0",
                    "type": "3"
                }
            ]
        }
    ]
}

END

阅读更多

扫码向博主提问

去开通我的Chat快问

qq_29202427

非学,无以致疑;非问,无以广识
个人分类: PHP接口 PHP后台
上一篇php关于五级分销的操作
下一篇自己封装的工具,自己给自己项目造轮子
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭