• PHP 之实现按日期进行分组、分页


    一、效果图

    二、原始数据

    array(6) {
      [0]=>
      array(8) {
        ["id"]=>
        string(1) "6"
        ["userid"]=>
        string(1) "1"
        ["friend_id"]=>
        string(3) "843"
        ["content"]=>
        string(10) "你好啊
    "
        ["date_entered"]=>
        string(19) "2019-08-01 08:42:07"
        ["thumb"]=>
        string(116) "http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0"
        ["visittime"]=>
        int(1564620127)
        ["date"]=>
        string(5) "08:42"
      }
      [1]=>
      array(8) {
        ["id"]=>
        string(1) "5"
        ["userid"]=>
        string(1) "1"
        ["friend_id"]=>
        string(3) "843"
        ["content"]=>
        string(6) "哈哈"
        ["date_entered"]=>
        string(19) "2019-08-01 08:41:58"
        ["thumb"]=>
        string(116) "http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0"
        ["visittime"]=>
        int(1564620118)
        ["date"]=>
        string(5) "08:41"
      }
      [2]=>
      array(8) {
        ["id"]=>
        string(1) "4"
        ["userid"]=>
        string(1) "1"
        ["friend_id"]=>
        string(3) "843"
        ["content"]=>
        string(11) "8月1好啦"
        ["date_entered"]=>
        string(19) "2019-08-01 08:41:47"
        ["thumb"]=>
        string(116) "http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0"
        ["visittime"]=>
        int(1564620107)
        ["date"]=>
        string(5) "08:41"
      }
      [3]=>
      array(8) {
        ["id"]=>
        string(1) "3"
        ["userid"]=>
        string(1) "1"
        ["friend_id"]=>
        string(3) "843"
        ["content"]=>
        string(3) "456"
        ["date_entered"]=>
        string(19) "2019-07-31 15:51:17"
        ["thumb"]=>
        string(116) "http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0"
        ["visittime"]=>
        int(1564559477)
        ["date"]=>
        string(5) "15:51"
      }
      [4]=>
      array(8) {
        ["id"]=>
        string(1) "2"
        ["userid"]=>
        string(1) "1"
        ["friend_id"]=>
        string(3) "843"
        ["content"]=>
        string(3) "123"
        ["date_entered"]=>
        string(19) "2019-07-31 15:50:23"
        ["thumb"]=>
        string(116) "http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0"
        ["visittime"]=>
        int(1564559423)
        ["date"]=>
        string(5) "15:50"
      }
      [5]=>
      array(8) {
        ["id"]=>
        string(1) "1"
        ["userid"]=>
        string(1) "1"
        ["friend_id"]=>
        string(3) "843"
        ["content"]=>
        string(9) "开始啦"
        ["date_entered"]=>
        string(19) "2019-07-31 15:47:24"
        ["thumb"]=>
        string(116) "http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0"
        ["visittime"]=>
        int(1564559244)
        ["date"]=>
        string(5) "15:47"
      }
    }

    三、PHP处理函数

    function groupVisit($visit)
    {
        $curyear = date('Y');
        $visit_list = [];
        foreach ($visit as $v) {
            if ($curyear == date('Y', $v['visittime'])) {
                $date = date('m月d日', $v['visittime']);
            } else {
                $date = date('Y年m月d日', $v['visittime']);
            }
            $visit_list[$date][] = $v;
        }
        return $visit_list;
    }

    处理后数据:

    array(2) {
      ["08月01日"]=>
      array(3) {
        [0]=>
        array(8) {
          ["id"]=>
          string(1) "6"
          ["userid"]=>
          string(1) "1"
          ["friend_id"]=>
          string(3) "843"
          ["content"]=>
          string(10) "你好啊
    "
          ["date_entered"]=>
          string(19) "2019-08-01 08:42:07"
          ["thumb"]=>
          string(116) "http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0"
          ["visittime"]=>
          int(1564620127)
          ["date"]=>
          string(5) "08:42"
        }
        [1]=>
        array(8) {
          ["id"]=>
          string(1) "5"
          ["userid"]=>
          string(1) "1"
          ["friend_id"]=>
          string(3) "843"
          ["content"]=>
          string(6) "哈哈"
          ["date_entered"]=>
          string(19) "2019-08-01 08:41:58"
          ["thumb"]=>
          string(116) "http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0"
          ["visittime"]=>
          int(1564620118)
          ["date"]=>
          string(5) "08:41"
        }
        [2]=>
        array(8) {
          ["id"]=>
          string(1) "4"
          ["userid"]=>
          string(1) "1"
          ["friend_id"]=>
          string(3) "843"
          ["content"]=>
          string(11) "8月1好啦"
          ["date_entered"]=>
          string(19) "2019-08-01 08:41:47"
          ["thumb"]=>
          string(116) "http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0"
          ["visittime"]=>
          int(1564620107)
          ["date"]=>
          string(5) "08:41"
        }
      }
      ["07月31日"]=>
      array(3) {
        [0]=>
        array(8) {
          ["id"]=>
          string(1) "3"
          ["userid"]=>
          string(1) "1"
          ["friend_id"]=>
          string(3) "843"
          ["content"]=>
          string(3) "456"
          ["date_entered"]=>
          string(19) "2019-07-31 15:51:17"
          ["thumb"]=>
          string(116) "http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0"
          ["visittime"]=>
          int(1564559477)
          ["date"]=>
          string(5) "15:51"
        }
        [1]=>
        array(8) {
          ["id"]=>
          string(1) "2"
          ["userid"]=>
          string(1) "1"
          ["friend_id"]=>
          string(3) "843"
          ["content"]=>
          string(3) "123"
          ["date_entered"]=>
          string(19) "2019-07-31 15:50:23"
          ["thumb"]=>
          string(116) "http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0"
          ["visittime"]=>
          int(1564559423)
          ["date"]=>
          string(5) "15:50"
        }
        [2]=>
        array(8) {
          ["id"]=>
          string(1) "1"
          ["userid"]=>
          string(1) "1"
          ["friend_id"]=>
          string(3) "843"
          ["content"]=>
          string(9) "开始啦"
          ["date_entered"]=>
          string(19) "2019-07-31 15:47:24"
          ["thumb"]=>
          string(116) "http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0"
          ["visittime"]=>
          int(1564559244)
          ["date"]=>
          string(5) "15:47"
        }
      }
    }
    {"08u670801u65e5":[{"id":"6","userid":"1","friend_id":"843","content":"u4f60u597du554a
    ","date_entered":"2019-08-01 08:42:07","thumb":"http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0","visittime":1564620127,"date":"08:42"},{"id":"5","userid":"1","friend_id":"843","content":"u54c8u54c8","date_entered":"2019-08-01 08:41:58","thumb":"http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0","visittime":1564620118,"date":"08:41"},{"id":"4","userid":"1","friend_id":"843","content":"8u67081u597du5566","date_entered":"2019-08-01 08:41:47","thumb":"http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0","visittime":1564620107,"date":"08:41"}],"07u670831u65e5":[{"id":"3","userid":"1","friend_id":"843","content":"456","date_entered":"2019-07-31 15:51:17","thumb":"http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0","visittime":1564559477,"date":"15:51"},{"id":"2","userid":"1","friend_id":"843","content":"123","date_entered":"2019-07-31 15:50:23","thumb":"http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0","visittime":1564559423,"date":"15:50"},{"id":"1","userid":"1","friend_id":"843","content":"u5f00u59cbu5566","date_entered":"2019-07-31 15:47:24","thumb":"http://wx.qlogo.cn/mmopen/PiajxSqBRaEJKa1QdSvmwdHzRA6Kw4O72ItIxNJTbvwP6eUcIxIFjr6pmnMYOPzAZLxXB4xFfn63s1iaAYV8Zfbw/0","visittime":1564559244,"date":"15:47"}]}

    四、前端部分代码

    var result = "";
                        for (var i = 0; i < date.length; i++) {
                            //判断是否在数组中存在,存在则追加列表,不存在则显示追加日期
                            if (contains(arr2, date[i]) == false) {
                                result += `<li class="aui-list-header" style="background: #fff;line-height: inherit;border-bottom: 1px solid #f5f5f5;padding: .5rem 0 .5rem .75rem;font-size: .7rem;">
                                    ` + date[i] + `
                                </li>`;
                                arr2.push(date[i]);
                            }
                            for (var j = 0; j < data[date[i]].length; j++) {
                                result += `<li class="aui-list-item">
                                    <div class="aui-media-list-item-inner">
                                        <div class="aui-list-item-media" style=" 3rem;text-align: center;">
                                            <img class="icon-item" src="` + data[date[i]][j].thumb + `" alt="">
                                        </div>
                                        <div class="aui-list-item-inner" style="align-items: center;">
                                            <div class="aui-list-item-text" style="height: 100%;">
                                                <div class="aui-list-item-title" style="font-size: .7rem !important;">新增记录:<span style="color: #f00;">` + data[date[i]][j].content + `</span></div>
                                                <div class="aui-list-item-right user-come" style="font-size: .6rem !important;">` + data[date[i]][j].date + `</div>
                                            </div>
                                        </div>
                                    </div>
                                </li>`;
                            }
                        }
  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    数据库作业5:SQL练习2
    数据库作业4:SQL练习1
    Linux的cp复制和mv移动命令的超详细介绍
    python数组添加整行或整列
    CentOS7下安装tomcat的超详细说明
  • 原文地址:https://www.cnblogs.com/yang-2018/p/11281265.html
Copyright © 2020-2023  润新知