How to omit the first row of an xlsx file

0

I'm using SimpleXLSX to import excel files into my database, but I do not want the first line to be added, try using the continue but what it does is insert all the data in the table twice

Here is the code

if (isset($_FILES['file'])) {

 require_once __DIR__ . '/simplexlsx.class.php';

 if ( $xlsx = SimpleXLSX::parse( $_FILES['file']['tmp_name'] ) ) {
  $stmt = $conn->prepare( "INSERT INTO paises (item, codigo_sap, descripcion, und, menor_precio, peso, volumen, provmenor, paisprov, nproceso, emprescon, econtra, fecha) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  $stmt->bindParam( 1, $item);
  $stmt->bindParam( 2, $codsap);
  $stmt->bindParam( 3, $desc);
  $stmt->bindParam( 4, $und);
  $stmt->bindParam( 5, $menor);
  $stmt->bindParam( 6, $peso);
  $stmt->bindParam( 7, $volumen);
  $stmt->bindParam( 8, $provme);
  $stmt->bindParam( 9, $provpis);
  $stmt->bindParam( 10, $proceso);
  $stmt->bindParam( 11, $econcur);
  $stmt->bindParam( 12, $econtr);
  $stmt->bindParam( 13, $fecha);
    foreach ( $xlsx->rows() as $fields ) {
        if($fields== 1){ $fields++; continue; }
        $item = $fields[0];
        $codsap = $fields[1];
        $desc = $fields[2];
        $und = $fields[3];
        $menor = $fields[4];
        $peso = $fields[5];
        $volumen = $fields[6];
        $provme = $fields[7];
        $provpis = $fields[8];
        $proceso = $fields[9];
        $econcur = $fields[10];
        $econtr = $fields[11];
        $fecha = $fields[12];
        $stmt->execute();

    }

} else {
    echo SimpleXLSX::parse_error();
}
    
asked by Maria 19.07.2018 в 20:45
source

3 answers

1

I think the most efficient thing would be to simply discard the first row of the array returned by the rows method, in this way you avoid that in each cycle an if is executed that does not contribute anything, that is:

// Obtienes el array
$filas = $xlsx->rows();
// eliminas el indice 0 (la primera fila)
unset($filas[0]);
// recorres el array
foreach ( $fila as $fields ) {
    $item = $fields[0];
    $codsap = $fields[1];
    $desc = $fields[2];
    $und = $fields[3];
    $menor = $fields[4];
    $peso = $fields[5];
    $volumen = $fields[6];
    $provme = $fields[7];
    $provpis = $fields[8];
    $proceso = $fields[9];
    $econcur = $fields[10];
    $econtr = $fields[11];
    $fecha = $fields[12];
    $stmt->execute();

}
    
answered by 19.07.2018 / 21:10
source
0

Try it like this:

foreach ( $xlsx->rows() as $i => $fields ) {
    if($i== 0){ continue; }
    $item = $fields[0];
    $codsap = $fields[1];
    $desc = $fields[2];
    $und = $fields[3];
    $menor = $fields[4];
    $peso = $fields[5];
    $volumen = $fields[6];
    $provme = $fields[7];
    $provpis = $fields[8];
    $proceso = $fields[9];
    $econcur = $fields[10];
    $econtr = $fields[11];
    $fecha = $fields[12];
    $stmt->execute();

}
    
answered by 19.07.2018 в 20:47
0

Includes the registration of the data inside the if and jumps the $ i == 0

foreach ( $xlsx->rows() as $i => $fields ) {
    if($i> 0){
    $item = $fields[0];
    $codsap = $fields[1];
    $desc = $fields[2];
    $und = $fields[3];
    $menor = $fields[4];
    $peso = $fields[5];
    $volumen = $fields[6];
    $provme = $fields[7];
    $provpis = $fields[8];
    $proceso = $fields[9];
    $econcur = $fields[10];
    $econtr = $fields[11];
    $fecha = $fields[12];
    $stmt->execute();
   }
}

Try this

    
answered by 19.07.2018 в 20:59