簡體   English   中英

生成后如何在excel上顯示數據值? Codeigniter PHP

[英]How to display data values on excel after generated? Codeigniter PHP

我正在嘗試將數據庫中的值顯示到 excel 表,問題是我有一個undefined offset error

問題從這里開始:

代碼: enrollmentID是我識別每個值的關鍵,foreach 是未定義偏移量所在的問題。 有沒有辦法解決這個問題,以便我可以在 excel 中顯示我的數據?

if($key == 'enrollmentID'){
    $intLec = 0;                    
    $intLab = 0;                                     

    foreach ($enrollmentSubjectListData[$value]['subjectCode'] as $subjKey => $subjValue) {
        $coordinates = $this->getExcelColumnConversion($col) . $row;
        $this->excel->getActiveSheet()->setCellValue($coordinates, $subjValue);
        $col++; 

        $coordinates = $this->getExcelColumnConversion($col) . $row;
        $this->excel->getActiveSheet()->setCellValue($coordinates, $enrollmentSubjectListData[$value]['numericalEquivalent'][$subjKey]);
        $col++; 
        
        if($enrollmentSubjectListData[$value]['lab'][$subjKey] != 0)
            $units = $enrollmentSubjectListData[$value]['lec'][$subjKey] . '/' . $enrollmentSubjectListData[$value]['lab'][$subjKey];
        else
            $units = $enrollmentSubjectListData[$value]['lec'][$subjKey];

        $coordinates = $this->getExcelColumnConversion($col) . $row;
        $this->excel->getActiveSheet()->setCellValue($coordinates, $units);
        $col++;

        $intLec += (float)str_replace(array( '(', ')' ), '', $enrollmentSubjectListData[$value]['lec'][$subjKey]);                                                                                        
        $intLab += (float)$enrollmentSubjectListData[$value]['lab'][$subjKey];
    }
    
    if($intLab == 0){
        $coordinates = $this->getExcelColumnConversion($col) . $row;
        $this->excel->getActiveSheet()->setCellValue($coordinates, $intLec);
    }
    else{
        $coordinates = $this->getExcelColumnConversion($col) . $row;
        $this->excel->getActiveSheet()->setCellValue($coordinates, $intLec . '/' . $intLab);
    }
}

undefined offset錯誤通常意味着您嘗試在數組中檢索的鍵不存在。

在這里,如果在foreach語句中拋出錯誤,原因可能是:

  • $enrollmentSubjectListData數組中不存在 $value 鍵
  • 或者, $enrollmentSubjectListData[$value]不包含'subjectCode'鍵(或者只是$enrollmentSubjectListData[$value]不是數組)

您可以實施的一個簡單修復方法是用IF語句包圍您的foreach

// Making sure that the $value key exists in the $enrollmentSubjectListData array
    if(key_exists($value,$enrollmentSubjectListData))
    {
        $valueArray = $enrollmentSubjectListData[$value];
    
        // Making sure that the $valueArray is an array and that the $valueArray['subjectCode'] key exists
        if(is_array($valueArray) && (key_exists('subjectCode',$valueArray)))
        {
            $subjectCodeArray = $valueArray['subjectCode'];
    
            // Making sure that the $subjectCodeArray is an array
                if(is_array($subjectCodeArray))
                {
                    foreach ($subjectCodeArray as $subjKey => $subjValue) {
                        $coordinates = $this->getExcelColumnConversion($col) . $row;
                        $this->excel->getActiveSheet()->setCellValue($coordinates, $subjValue);
                        $col++; 
                    
                        $coordinates = $this->getExcelColumnConversion($col) . $row;
                        $this->excel->getActiveSheet()->setCellValue($coordinates, $enrollmentSubjectListData[$value]['numericalEquivalent'][$subjKey]);
                        $col++; 
                        
                        if($enrollmentSubjectListData[$value]['lab'][$subjKey] != 0)
                            $units = $enrollmentSubjectListData[$value]['lec'][$subjKey] . '/' . $enrollmentSubjectListData[$value]['lab'][$subjKey];
                        else
                            $units = $enrollmentSubjectListData[$value]['lec'][$subjKey];
                    
                        $coordinates = $this->getExcelColumnConversion($col) . $row;
                        $this->excel->getActiveSheet()->setCellValue($coordinates, $units);
                        $col++;
                    
                        $intLec += (float)str_replace(array( '(', ')' ), '', $enrollmentSubjectListData[$value]['lec'][$subjKey]);                                                                                        
                        $intLab += (float)$enrollmentSubjectListData[$value]['lab'][$subjKey];
                    }
                }
            }
        }

請記住,這段代碼只會防止您的代碼崩潰,但不會告訴您為什么您的輸入數據首先沒有正確格式化。

為此,我建議您在 IDE 中使用調試器,例如 xDebug。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM