Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shaddark's avatar

PHP Spreadsheet - check empty rows

Hello i'm using PHP Spreadsheet for get cells values from excel starting from row 9 and i have this part of code:

$row = 9;

if ($worksheet->getCell("A$row")->getValue() == NULL) {
    return redirect()->back()->with('error', 'Attenzione! '.  'la riga ' . $row .' non può essere vuota!');
} else {
    while ($worksheet->getCell("A$row")->getValue() !== NULL) {
        $newEntry = [];    
        $newEntry['codice_articolo'] = trim($worksheet->getCell("A$row")->getValue());  
        $newEntry['quantita'] = trim($worksheet->getCell("B$row")->getValue());      
        $newEntry['prezzo'] = number_format(floatval(str_replace(',','.',trim($worksheet->getCell("C$row")->getValue()))), 4, '.', ''); // CONVERTIRE SEPARATORE DALLA VIRGOLA AL PUNTO
        $db_cod_art = DB::connection('oracle')->table('orders')->where(DB::raw("TRIM(cod_art)"), trim($newEntry['codice_articolo']))->count('cod_art'); // SE RITORNA 1 IL CODICE ESISTE
        if ($db_cod_art != 1) {
            return redirect()->back()->with('error', 'Attenzione! '. 'Il codice articolo '. $newEntry['codice_articolo']  .' alla riga ' . $row .' articolo non esiste!');
        } else {
            $row++;
            $data_detail[] = $newEntry;
            if ($worksheet->getCell("A$row")->getValue() == NULL) {
                return redirect()->back()->with('error', 'Attenzione! '.  'la riga ' . $row .' non può essere vuota!');
            }
        }
    }       
}

Basically this code is working for each empty row. But how should i do for the last row? Last row would always be empty, because you can't know how many record has the column A in the excel.

0 likes
1 reply

Please or to participate in this conversation.