如何在Excel上显示生成后的数据值?Codeigniter PHP

7ajki6be  于 2023-01-28  发布在  PHP
关注(0)|答案(1)|浏览(179)

我尝试将数据库中的值显示到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);
    }
}
zysjyyx4

zysjyyx41#

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之类的调试器。

相关问题