<?php require 'mysql.php'; function utf2iso ($string) { $output = ''; $j = 0; for ($i=0; $i < $length; $i++) { if (ord($string[$i]) == 197) { switch (ord($string[$i+1])) { case 129: $output .= 'Ł'; break; case 130: $output .= 'ł'; break; case 155: $output .= 'ś'; break; case 187: $output .= 'Ż'; break; case 188: $output .= 'ż'; break; } $i++; } else if (ord($string[$i]) == 195) { switch (ord($string[$i+1])) { case 179: $output .= 'ó'; break; } $i++; } else if (ord($string[$i]) == 196) { switch (ord($string[$i+1])) { case 133: $output .= 'ą'; break; case 153: $output .= 'ę'; break; } $i++; } else if (ord($string[$i]) == 226 || ord($string[$i]) == 128 || ord($string[$i]) == 138) { $output .= '.'; } else $output .= $string[$i]; } } $xml = new XMLReader(); $xml->open('cennik.xml'); $super = ''; $prod = ''; while ($xml->read()) { switch ($xml->name) { case 'GrupaGlowna': mysql_query ("REPLACE INTO sklep_grupa VALUES ('" . $xml->getAttribute("id") . "', '', '" . utf2iso($xml->getAttribute("nazwa")) . "');") or die (mysql_error()); $super = $xml->getAttribute("id"); break; case 'PodGrupa': mysql_query ("REPLACE INTO sklep_grupa VALUES ('" . $xml->getAttribute("id") . "', '" . $super . "', '" . utf2iso($xml->getAttribute("nazwa")) . "');") or die (mysql_error()); break; case 'producent': mysql_query ("REPLACE INTO sklep_producent VALUES ('" . $xml->getAttribute("id") . "', '" . htmlspecialchars(utf2iso($xml->getAttribute("nazwa"))) . "');") or die (mysql_error()); break; case 'produkt': mysql_query ("REPLACE INTO sklep_produkt VALUES ('" . $xml->getAttribute("id") . "', '" . utf2iso($xml->getAttribute("nazwa")) . "', '" . $xml->getAttribute("producent") . "', '" . $xml->getAttribute("grupa") . "', '" . $xml->getAttribute("gwarancja") . "', '" . $xml->getAttribute("cena_netto") . "', '" . ($xml->getAttribute("cena_netto")*1.22) . "', '" . $xml->getAttribute("dostepny") . "', '" . $xml->getAttribute("data") . "');") or die (mysql_error()); $prod = $xml->getAttribute("id"); break; case 'zdjecie': mysql_query ("REPLACE INTO sklep_zdjecie VALUES ('$prod', '" . $xml->getAttribute("plik") . "', '" . $xml->getAttribute("domyslne") . "', '" . $xml->getAttribute("data") . "');") or die (mysql_error()); break; case 'parametr': mysql_query ("REPLACE INTO sklep_parametr VALUES ('$prod', '" . utf2iso($xml->getAttribute("nazwa")) . "', '" . utf2iso($xml->getAttribute("opis")) . "', '" . $xml->getAttribute("jm") . "');") or die (mysql_error()); break; default: } } ?>
Dodam, że funkcja utf2iso musi istnieć, ponieważ iconv" title="Zobacz w manualu PHP" target="_manual sobie nie radził z niektórymi znakami. Dodatkowo nie wiedząc czemu, rekordy wstawiały się dwukrotnie - stąd użycie odpowiednich kluczy w bazie danych i zapytania typu REPLACE.
Czy w powyższym kodzie można coś poprawić co ewentualnie zwiększyłoby jego wydajność (skróciło czas wykonania)? Obecnie wynosi on ok. 180 sekund!