<?
require('config.php');
if(isset($_GET['i'])) $start_i = $_GET['i']; else $start_i = 1;
if(isset($_GET['j'])) $start_j = $_GET['j']; else $start_j = 2;
if(isset($_GET['cid'])) $country_id = $_GET['cid'];
$str = file_get_contents('http://jdu.ru/search_city.phtml');
preg_match("/<select name=\"country_id\".*>(.*)<\/select>/sU", $str, $country_body);
preg_match_all("/<option value=\"(\w+)\".*>(.+)<\/option>/sU", $country_body[1], $country_m);
for($i = $start_i; $i < sizeof($country_m[2]); $i++) {
$region = file_get_contents('http://jdu.ru/search_city.phtml?country_id='. $country_m[1][$i]);
preg_match("/<select name=\"region_id\".*>(.*)<\/select>/sU", $region, $reg_body);
preg_match_all("/<option value=\"(\w+)\".*>(.*)<\/option>/sU", $reg_body[1], $reg_m);
mysql_query("INSERT INTO `country`(`name`) VALUES ('". $country_m[2][$i]. "')");
if($iid = mysql_insert_id()) $country_id = $iid;
for($j = $start_j; $j < sizeof($reg_m[2]); $j++) {
$city = file_get_contents('http://jdu.ru/search_city.phtml?country_id='. $country_m[1][$i]. "®ion_id=". $reg_m[1][$j]);
preg_match("/<select name=\"city_id\".*>(.*)<\/select>/sU", $city, $city_body);
preg_match_all("/<option value=\"(\w+)\".*>(.*)<\/option>/sU", $city_body[1], $city_m);
mysql_query("INSERT INTO `region` VALUES ('', '$country_id', '".$reg_m[2][$j]. "')");
$region_id = mysql_insert_id();
for($k = 2; $k < sizeof($city_m[2]); $k++) {
mysql_query("INSERT INTO `city` VALUES ('', '$region_id', '". $city_m[2][$k]. "')");
}
$start_j++;
break;
}
if($j == sizeof($reg_m[2])) {
$start_i++;
$start_j = 2;
}
break;
}
echo "<body onLoad=\"document.location = 'test.php?i=$start_i&j=$start_j&cid=$country_id';\">";
?>