How can you create an array to scan results from a mysql query in php?

0

The small php code below gives me the result of a query in column A, B, Q in excel. The problem now is that I need to make an arrangement or cycle in which it is traversed according to the column in the that is found to fill the other columns of the C: P. Claro making use of another query not the same at first.

$i = 5;
        while ($fila = $total->fetch_array()) {
            $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A'.$i,  $fila['gasto'])
                    ->setCellValue('B'.$i,  $fila['lunes'])

                    ->setCellValue('Q'.$i, utf8_encode($fila['factura']));
                    $i++;   
        }
    
asked by Madaley 17.07.2018 в 19:24
source

1 answer

0

It is difficult without knowing how you are going to get the data for the other columns, but I would make two arrangements and thus populate the other cells, something like this:

$i = 5;
$columnas = ['C', 'D', 'E', ... , 'P'];
$campos = ['martes', 'miercoles', ... ]; // Ese arreglo contiene todas las columnas que vas a usar
    while ($fila = $total->fetch_array()) {
        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A'.$i,  $fila['gasto'])
                ->setCellValue('B'.$i,  $fila['lunes'])

                ->setCellValue('Q'.$i, utf8_encode($fila['factura']));
                $fila2 = // Aqui consigues los datos a rellenar
                for ($j = 0; $j < 14; $j++) {
                    $objPHPExcel->setCellValue($columnas[$j].$i, $fila2[$campos[$j]]);
                }
                $i++;   
    }
    
answered by 17.07.2018 в 19:54