简体   繁体   中英

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

The problem starts here:

Code: The enrollmentID is my key to identify each value, the foreach is the problem where the undefined offset is. Is there a way to fix this so I can display my data in 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.

Here, if the error is thrown in the foreach statement, the reason could either be that:

  • The $value key does not exist in the $enrollmentSubjectListData array
  • Or, that the $enrollmentSubjectListData[$value] does not contain a 'subjectCode' key (or simply that $enrollmentSubjectListData[$value] is not an array)

One simple fix that you could implement is to surround your foreach by an IF statement:

// 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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