简体   繁体   English

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

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

I am trying to display the values from database to excel sheet, the problem is that I am having an undefined offset error我正在尝试将数据库中的值显示到 excel 表,问题是我有一个undefined offset error

The problem starts here:问题从这里开始:

Code: The enrollmentID is my key to identify each value, the foreach is the problem where the undefined offset is.代码: enrollmentID是我识别每个值的关键,foreach 是未定义偏移量所在的问题。 Is there a way to fix this so I can display my data in excel?有没有办法解决这个问题,以便我可以在 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);
    }
}

An undefined offset error generally means that the key you are trying to retrieve in your array does not exist. undefined offset错误通常意味着您尝试在数组中检索的键不存在。

Here, if the error is thrown in the foreach statement, the reason could either be that:在这里,如果在foreach语句中抛出错误,原因可能是:

  • The $value key does not exist in the $enrollmentSubjectListData array $enrollmentSubjectListData数组中不存在 $value 键
  • Or, that the $enrollmentSubjectListData[$value] does not contain a 'subjectCode' key (or simply that $enrollmentSubjectListData[$value] is not an array)或者, $enrollmentSubjectListData[$value]不包含'subjectCode'键(或者只是$enrollmentSubjectListData[$value]不是数组)

One simple fix that you could implement is to surround your foreach by an IF statement:您可以实施的一个简单修复方法是用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];
                    }
                }
            }
        }

Just keep in mind that this snippet of code will only prevent your code from crashing but it will not tell you why your input data was not properly formatted in the first place.请记住,这段代码只会防止您的代码崩溃,但不会告诉您为什么您的输入数据首先没有正确格式化。

For that, I will suggest you to use a debugger such as xDebug in your IDE.为此,我建议您在 IDE 中使用调试器,例如 xDebug。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM