Jump to content
  • 0

Проблема с кодировкой


dinaron
 Share

Question

Добрый вечер.

у меня следующий вопрос, в инете нашел один скрипт который осуществляет выбор страны, региона и города соответственно.

Данный скрипт я хочу переделать и вставить в свой сайт. Проблема в следующем, у меня не получается запустить работу скрипта в кодировке utf-8. Преобразовывал бд, все страницы в utf-8, результата никакого. Метод тыка удалось найти конкретную проблему изменений. Поменяв все на utf-8, кроме строки в cities.php "header('Content-type: text/xml; charset=windows-1251'); " скрипт работает, но только как я поменяю windows-1251 на utf-8, выборка из бд не осуществляется.

index.php


<?php header('Content-type: text/html; charset=utf-8')?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Выберите город</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script language="javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.post("cities.php", {},
function (xml) {
$(xml).find('country').each(function() {
id = $(this).find('id_country').text();
$("#country").append("<option value='" + id + "'>" + $(this).find('country_name_ru').text() + "</option>");
});
});
$("#country").change(function() {
id_country = $("#country option:selected").val();
if (id_country == "") {
$(".region, .city, #submit").hide();
}else {
$("#region").html('');
$("#region").html('<option value="">Выберите регион</option>');
$.post("cities.php", {id_country: id_country},
function (xml) {
$(xml).find('region').each(function() {
id = $(this).find('id_region').text();
$("#region").append("<option value='" + id + "'>" + $(this).find('region_name_ru').text() + "</option>");
});
});
$(".region").show();
}
});
$("#region").change(function() {
id_region = $("#region option:selected").val();
if (id_region == "") {
$(".city").hide();
}else {
$("#city").html('');
$("#city").html('<option value="">Выберите город</option>');
$.post("cities.php", {id_region: id_region},
function (xml) {
$(xml).find('city').each(function() {
id = $(this).find('id_city').text();
$("#city").append("<option value='" + id + "'>" + $(this).find('city_name_ru').text() + "</option>");
});
});
}
$(".city").show();
});
$("#city").change(function() {
if ($("#city option:selected").val() == "") {
$("#submit").hide();
}else {
$("#submit").show();
}
});
});
</script>
<style>
.region, .city, #submit {display:none}
strong {display:block;width:50px}
div {margin-bottom:3px}
select {width:200px}
</style>
</head>
<body>
<form action="" method="POST">
<div>
<strong>Страна:</strong>
<select name="country" id="country">
<option value="" selected>Выберите страну</option>
</select>
</div>
<div class="region">
<strong>Регион:</strong>
<select name="region" id="region">
<option value="">Выберите регион</option>
</select>
</div>
<div class="city">
<strong>Город:</strong>
<select name="city" id="city">
<option value="">Выберите город</option>
</select>
</div>
<div>
<input type="submit" id="submit" value="Сохранить">
</div>
</form>
</body>
</html>

cities.php


<?php


header('Content-type: text/xml; charset=windows-1251');

$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'cities';

$cities = new cities($db_host, $db_user, $db_pass, $db_name);
if (isset($_POST['id_country'])) {
$cities->get_regions($_POST['id_country']);
}elseif (isset($_POST['id_region'])) {
$cities->get_cities($_POST['id_region']);
}
else {
$cities->get_countries();
}

class cities {
private $db;
public $xml = '<?xml version="1.0" encoding="utf-8"?><root>';

function __construct($db_host, $db_user, $db_pass, $db_name) {
$this->db = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name, $this->db);
}

function get_countries() {
$mysql_result = mysql_query("SELECT * FROM countries ORDER BY country_order", $this->db);
while($country = mysql_fetch_assoc($mysql_result)) {
$this->xml .= '<country>';
$this->xml .= '<id_country>'.$country['id_country'].'</id_country>';
$this->xml .= '<country_name_ru><![CDATA['.$country['country_name_ru'].']]></country_name_ru>';
$this->xml .= '<country_name_en><![CDATA['.$country['country_name_en'].']]></country_name_en>';
$this->xml .= '<iso>'.$country['country_iso'].'</iso>';
$this->xml .= '</country>';
}
$this->print_xml();
}

function get_regions($id_country) {
$mysql_result = mysql_query("SELECT * FROM regions WHERE id_country = ".intval($id_country)." ORDER BY region_order", $this->db);
while($region = mysql_fetch_assoc($mysql_result)) {
$this->xml .= '<region>';
$this->xml .= '<id_region>'.$region['id_region'].'</id_region>';
$this->xml .= '<region_name_ru><![CDATA['.$region['region_name_ru'].']]></region_name_ru>';
$this->xml .= '<region_name_en><![CDATA['.$region['region_name_en'].']]></region_name_en>';
$this->xml .= '</region>';
}
$this->print_xml();
}

function get_cities($id_region) {
$mysql_result = mysql_query("SELECT * FROM cities WHERE id_region = ".intval($id_region)." ORDER BY city_order", $this->db);
while($city = mysql_fetch_assoc($mysql_result)) {
$this->xml .= '<city>';
$this->xml .= '<id_city>'.$city['id_city'].'</id_city>';
$this->xml .= '<city_name_ru><![CDATA['.$city['city_name_ru'].']]></city_name_ru>';
$this->xml .= '<city_name_en><![CDATA['.$city['city_name_en'].']]></city_name_en>';
$this->xml .= '</city>';
}
$this->print_xml();
}

function print_xml() {
$this->xml .= '</root>';
echo $this->xml;
}
}

ссылка на бд

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

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. See more about our Guidelines and Privacy Policy