這篇文章介紹的內容是關於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){ $re[$a]['list'] = $va; } } if($list){ $data = $re; }else{ $data = []; } return success($data); } }
這裡是結果
{ "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": "直播售賣" } ] } ]}
END