Reputation:
As a title I have implemented within my project, a part in which the user can download a CSV model, and through pre-established fields upload their products in multiple ways, so as to make the work much faster.
The loading in CSV, works perfectly, plus I implemented within this loading also a check on a given product, so that in case they had to load a product with a code already registered in the db, leave out an alert, which warns the user of the fact.
My question once explained the procedure is: when the user downloads the CSV document, and this is formed by: -First name -Code etc...
the first line how can I ignore it from the code and record all the products from the second on?
Code:
<?php
$ESITO_POSITIVO = "";
$ESITO_NEGATIVO = "";
if(isset($_POST["submit"]))
{
include '../../connessione.php';
if(!$connessione){
die('Could not Connect My Sql:' .mysqli_error());
}
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$category_id = $_POST['category_id'];
$concessionaria = $filesop[0];
$proprietaria = $filesop[1];
$cimasa = $filesop[2];
$address = $filesop[3];
$city = $filesop[4];
$cap = $filesop[5];
$lat = $filesop[6];
$lng = $filesop[7];
$inpe = $filesop[8];
$tipo_impianto = $filesop[9];
$tipologia = $filesop[10];
$illuminato = $filesop[11];
$numero_facce = $filesop[12];
$quartiere = $filesop[13];
$comune = $filesop[14];
$ubicazione = $filesop[15];
$circuito = $filesop[16];
$costo_quattordici = $filesop[17];
$costo_mese = $filesop[18];
$CONTROLLA = mysqli_query($connessione,"SELECT cimasa FROM store_locator WHERE cimasa='".$cimasa."'");
$SE_IL_RISULTATO_IMMESSO=mysqli_num_rows($CONTROLLA);
if($SE_IL_RISULTATO_IMMESSO==0) {
$connessione->query("insert into store_locator(category_id,concessionaria,proprietaria,cimasa,address,city,cap,lat,lng,inpe,tipo_impianto,tipologia,illuminato,numero_facce,quartiere,comune,ubicazione,circuito,costo_quattordici,costo_mese) values ('$category_id','$concessionaria','$proprietaria','$cimasa','$address','$city','$cap','$lat','$lng','$inpe','$tipo_impianto','$tipologia','$illuminato','$numero_facce','$quartiere','$comune','$ubicazione','$circuito','$costo_quattordici','$costo_mese')");
$stmt = mysqli_prepare($connessione);
mysqli_stmt_execute($stmt);
$c = $c + 1;
$ESITO_POSITIVO = '<div class="alert alert-success" role="alert">
<strong>Impianto registrato con successo!</strong><br>
<a href="../../gestisci/impianti/impianti.php" type="button" class="btn btn-success">Gestisci i tuoi Impianti</a>
</div>';
}
else
{
$ESITO_NEGATIVO = '<div class="alert alert-danger" role="alert">
<strong>Attenzione! all’interno del file che è stato caricato è presente un numero di Cimasa già registrato, si prega di controllare e ricaricare il file!</strong>
</div>';
}
}
}
?>
Upvotes: 0
Views: 28
Reputation: 5191
I usually just retrieve a row to be thrown away/ignored if I know that there is a header row and I don't want or need it.
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$trash = fgetcsv($handle, 1000, ","); // retrieve the header line and ignore it
$c = 0;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
Upvotes: 1
Reputation: 23001
You need to create a counter to increment in your loop. If the counter is 1, then it's the first row and you need to use "continue" to have it skip that iteration of the loop. You need to increment before continue, or it will never increment:
<?php
$ESITO_POSITIVO = "";
$ESITO_NEGATIVO = "";
if(isset($_POST["submit"]))
{
include '../../connessione.php';
if(!$connessione){
die('Could not Connect My Sql:' .mysqli_error());
}
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
$i = 0;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$i++;
if($i === 1) continue;
// Rest of code here
}
}
?>
Upvotes: 0