getConnection(); $stmt = $pdo->prepare(" SELECT p.id, p.iddatadb, p.part_number, p.part_description, p.material, p.color, p.mix, p.idmatrice, p.note, p.dateexpiry, a.NomeMatriceTraduzione FROM identification_parts p LEFT JOIN auth_matrici a ON a.IdMatrice = p.idmatrice WHERE p.iddatadb = ? ORDER BY CASE WHEN p.part_number IS NULL THEN 999999 ELSE p.part_number END ASC, p.id ASC "); $stmt->execute([$iddatadb]); $parts = $stmt->fetchAll(PDO::FETCH_ASSOC); /** * Build matrix groups from parts. * No join for now: we use idmatrice only. */ $matrixGroups = []; foreach ($parts as $part) { $matrixKey = (!empty($part['idmatrice'])) ? (string)$part['idmatrice'] : 'NO_MATRIX'; if (!isset($matrixGroups[$matrixKey])) { $matrixGroups[$matrixKey] = [ 'idmatrice' => $part['idmatrice'], 'NomeMatriceTraduzione' => $part['NomeMatriceTraduzione'] ?? '', 'parts_count' => 0, 'parts' => [], ]; } $matrixGroups[$matrixKey]['parts_count']++; $matrixGroups[$matrixKey]['parts'][] = $part['id']; } $matrixGroups = array_values($matrixGroups); ?>