mozack Posted December 18, 2009 at 02:14 PM Report Share #301261 Posted December 18, 2009 at 02:14 PM 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 More sharing options...
pedrotuga Posted December 19, 2009 at 04:39 PM Report Share #301442 Posted December 19, 2009 at 04:39 PM 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now