代码之家  ›  专栏  ›  技术社区  ›  Blues Clues

如何在laravel中从以前的记录中获取数据

  •  0
  • Blues Clues  · 技术社区  · 6 年前

    我有张桌子叫 items . 现在,我得到了本周的所有记录(意味着每天都有记录)。我已经做过了。

    但问题是,如果没有周三的记录,周三的数据应该还是周二或周三之前的数据。请看下面我的代码。

    public function arrayAssigner($theArray, $theArrayData, $field)
    {
        $format_to_day = function($date){
            return date_format(new \DateTime($date), 'D');
        };
    
        foreach($theArray as $key => $value){
            if(isset($theArrayData[$key]) && $format_to_day($theArrayData[$key]['created_at']) == 'Mon'){
                $theArray[0] = $theArrayData[$key][$field];
            } elseif (isset($theArrayData[$key]) && $format_to_day($theArrayData[$key]['created_at']) == 'Tue'){
                $theArray[1] = $theArrayData[$key][$field];
            } elseif (isset($theArrayData[$key]) && $format_to_day($theArrayData[$key]['created_at']) == 'Wed'){
                $theArray[2] = $theArrayData[$key][$field];
            } elseif (isset($theArrayData[$key]) && $format_to_day($theArrayData[$key]['created_at']) == 'Thu'){
                $theArray[3] = $theArrayData[$key][$field];
            } elseif (isset($theArrayData[$key]) && $format_to_day($theArrayData[$key]['created_at']) == 'Fri'){
                $theArray[4] = $theArrayData[$key][$field];
            }
        }
    
        return $theArray;
    }
    

    我是这样用的:

    $theArray = [0, 0, 0, 0, 0];
    $this->arrayAssigner($theArray, $dataOfTheWeek, 'quantity');
    

    我还有一个构造函数将weekstart设置为 星期一 .

    例子 :

    假设我有一个记录 Nov 12 - 16 . 现在,本周是 Nov 26-30 ,但在数据库中我没有 Nov 26 ,它的意思是,记录或数量 11月26日 记录来自 Nov 16 .

    输出(作为数组) :
    11月12日至16日:
    [7, 5, 6, 8, 12]

    11月26日- 30:
    [12, 8, 9, 10, 13]

    数组中的第一项 Nov 26 - 30 应该是12,因为它是 11月12日- 16 .

    支持图像:

    enter image description here

    结果必须是动态的;一旦有一天没有记录,它将填充最后一个记录。

    1 回复  |  直到 6 年前
        1
  •  0
  •   TalESid    6 年前
    public function arrayAssigner($theArray, $theArrayData, $field) {
    
        $format_to_day = function($date){
            return date_format(new \DateTime($date), 'D');
        };
    
        foreach($theArray as $key => $value) {
            if(isset($theArrayData[$key])) {
                $day = $format_to_day($theArrayData[$key]['created_at']);
    
                switch ($day) {
                    case 'Mon':
                        $theArray[0] = $theArrayData[$key][$field];
                    break;
                    case 'Tue':
                        // new_data  = ---if exists ---------------v--- assign ------------------v-- old_data--
                        $theArray[1] = $theArrayData[$key][$field] ? $theArrayData[$key][$field] : $theArray[0];
                    break;
                    case 'Wed':
                        // new_data  = ---if exists ---------------v--- assign ------------------v-- old_data--
                        $theArray[2] = $theArrayData[$key][$field] ? $theArrayData[$key][$field] : $theArray[1];
                    break;
                    case 'Thu':
                        // new_data  = ---if exists ---------------v--- assign ------------------v-- old_data--
                        $theArray[3] = $theArrayData[$key][$field] ? $theArrayData[$key][$field] : $theArray[2];
                    break;
                    case 'Fri':
                        // new_data  = ---if exists ---------------v--- assign ------------------v-- old_data--
                        $theArray[4] = $theArrayData[$key][$field] ? $theArrayData[$key][$field] : $theArray[3];
                    break;
                    default:
                    break;
                }
            }
        }
    
        return $theArray;
    }
    

    这就是你想要的吗????