查询每个月的所有数据记录

1.先功能需求的一个了解

2.wxml 部分 运用到 vantui

  <view wx:for="{{weightList}}" wx:for-index="index" wx:for-item="item" wx:key="weightList">

      <van-cell title="{{ index }}月" value="总变化{{ item.cha_num }}kg" border="{{ false }}" />

      <view wx:for="{{ item.list }}" wx:for-index="index" wx:for-item="iteme" wx:key="item.list ">
        <van-cell-group>

          <van-cell title="{{ iteme.create_time}}" value="{{ iteme.weight }}kg" border="{{ false }}" />

        </van-cell-group>
      </view>
    </view>

3.js部分  发起请求

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.request({
      url: 'http://tp51.com/index.php/testapi/Courses/weightList',
      success:(res)=>{
        console.log(res)
        that.setData({
          weightList : res.data.data
        })
      }
    })
  },

3.后端 方法 

  /**
     * 每个月 显示的数据
     * @return \think\response\Json
     */
    public function weightList()
    {
        $u_id = 1;
        //总共12个月
        $month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

        //查找每个月 对应记录的数据
        foreach ($month as $k => $v) {
            $list[$v]['list'] = Weight::where(['u_id' => $u_id, 'm_num' => $v])
                ->order('create_time', 'asc')
                ->field('m_num,create_time,weight')
                ->select()->toArray();

            //循环删除 为空的月份
            foreach ($list as $z => $j) {
                if ($j['list'] == null) {
                    unset($list[$z]);
                }else{

                    //找出最大体重 和最小体重
                    $difference =  Weight::where(['u_id' => $u_id, 'm_num' => $v])
                        ->group('m_num')
                        ->field('max(weight) as max_weight,min(weight) as min_weight')
                        ->select()->toArray();
                    // 将差集 循环添加到 数组中
                    foreach ($difference as $e) {
                        $list[$v]['cha_num'] = $e['max_weight'] - $e['min_weight'];
                    }
                }
            }
        }


        return json(['code' => 200, 'data' => $list]);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值