Contexte PHP sur le regroupement basé sur la même date

不言
Libérer: 2023-03-24 06:32:02
original
2023 Les gens l'ont consulté

Le contenu de cet article concerne le regroupement selon la même date en arrière-plan php. Maintenant, je le partage avec vous. Les amis dans le besoin peuvent s'y référer

接口中发现需要需要将每天(及时Unix时间不同),因为unix时间不同,需要将这个时间转成年月日这样,然后作为条件来查询这一天直播的历史数据。然后去这一天所有的数据,技术遇到的问题是,如何将这一天的数据放在一起,用sql分组不可以,因为uninx时间是不同的。所以我采用了函数循环比例进行分组。
    **没进行分组的代码**
Copier après la connexion
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);
         }
    }
Copier après la connexion
.
**这里没分组的结果**
{    "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": "直播售卖"
        }
    ]
}
Copier après la connexion


Ici sont regroupés selon end_time, et les données du même jour sont regroupées L'extrait de code

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){                    
            $re[$a]['list'] = $va;
                }
            }           
             if($list){                
             $data = $re;
            }else{                
            $data = [];
            }           
            return success($data);
         }
    }
Copier après la connexion
<🎜. >

Voici le résultat

{
    "status": "ok",
    "data": [
        {
            "time": "2018-04-12",
            "list": [
                {
                    "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": "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": "直播售卖"
                }
            ]        },
        {
            "time": "2018-04-13",
            "list": [
                {
                    "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": "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": "直播售卖"
                }
            ]        }
    ]}
Copier après la connexion
FIN

Recommandations associées :

Comment implémenter le démon d'arrière-plan PHP dans un environnement Linux

Comment implémenter l'arrière-plan PHP du mini-programme WeChat

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
php
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!