Jump to content

[DUVIDA] Dados de CSV para Base de dados


mozack
 Share

Recommended Posts

Pessoal,

Antes de mais quero avisar que sou novo por aqui, algum falha minha detectada pelos moderadores, por favor avisem.

Venho aqui pedir ajuda com php e ficheiros csv. Tenho uma loja online que actualiza as quantidades disponiveis dos produtos através de um ficheiro csv de outro site. O que acontece, o meu site liga-se ao outro, lê o csv e importa as quantidades disponiveis através do EAN, o csv tem 3 colunas:

1ª - EAN

2ª - Nome (mas esta não uso)

3ª - Quantidades

O que queria era que ao ler a terceira coluna, se o valor fosse igual ou inferior a 2 colocasse na bd 0, ou seja, com menos de 2 artigos no csv o valor inserido seria 0.

Fica aqui o meu código php (chamado por cronjob).

<?php 

// Connect to MySQL 
mysql_connect("localhost", "DBUSER", "DBPASS") or die(mysql_error()); 
mysql_select_db("DB") or die(mysql_error()); 

#if first row of csv file is headings set $row to 1. 
$row = 1; 
#database primary table 
$table_to_update = "TABELA_A_ACTUALIZAR"; 

#get the csv file 
$handle = fopen("NOMEDOFICHEIRO.csv", "r"); 

#go through the csv file and print each row with fields to the screen. 
#and import them into the database updating only the quantity  
while (($data = fgetcsv($handle, 100000, ";")) !== FALSE) { 
    $num = count($data); 
    echo "<p> $num fields in line $row: <br /></p>\n"; 
    $row++; 
    for ($c=0; $c < $num; $c++) { 
        if ($c = 1) { 
        $ean = $data[($c - 1)]; 
        echo $ean . " SKU Assigned <br />\n"; 
        } 
        if ($c = 3) { 
        $stock = $data[($c - 1)]; 
        mysql_query("UPDATE $table_to_update SET stock='$stock' WHERE ean='$ean'")  
        or die(mysql_error());  
        echo $stock . " Imported for row $row in product $ean <br />\n"; 
        } 
        // would have to add an additional  if statement for each field being updated and know the order of the fields from your csv file 
        //echo $data[$c] . "Imported <br />\n"; 
         
         
    } 
} 

fclose($handle); 
echo "<h1>Update Complete.</h1>"; 
?> 

Ok, agora tenho algumas dúvidas como "ler os dados" do csv e se for igual ou inferior a 2 colocar 0 na bd

Obrigado a todos

Link to comment
Share on other sites

No teu caso  só precisas de um if no sítio certo.

<?php
// Connect to MySQL
mysql_connect("localhost", "DBUSER", "DBPASS") or die(mysql_error());
mysql_select_db("DB") or die(mysql_error());
#if first row of csv file is headings set $row to 1.
$row = 1;
#database primary table
$table_to_update = "TABELA_A_ACTUALIZAR";
#get the csv file
$handle = fopen("NOMEDOFICHEIRO.csv", "r");
#go through the csv file and print each row with fields to the screen.
#and import them into the database updating only the quantity  
while (($data = fgetcsv($handle, 100000, ";")) !== FALSE) {
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        if ($c = 1) {
        $ean = $data[($c - 1)];
        echo $ean . " SKU Assigned <br />\n";
        }
        if ($c = 3) {
        $stock = $data[($c - 1)];
        
        if ($stock <=2){
        mysql_query("UPDATE $table_to_update SET stock='$stock' WHERE ean='$ean'")  
        or die(mysql_error());  
        echo $stock . " Imported for row $row in product $ean <br />\n";
        }
        else{
          echo "skipping row $row - (stock <= 3)<br />\n"
        }
        }
        // would have to add an additional  if statement for each field being updated and know the order of the fields from your csv file
        //echo $data[$c] . "Imported <br />\n";
         
         
    }
}
fclose($handle);
echo "<h1>Update Complete.</h1>";
?>

Esse código podia ser bem mais simplificado e mais fácil.

Não percebes ao certo como isso funciona pois não?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.