Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770
Форумы портала PHP.SU :: Версия для печати :: Зависимые списки Mysql
Форумы портала PHP.SU » » Работа с СУБД » Зависимые списки Mysql

Страниц (1): [1]
 

1. StaJLin2 - 03 Июня, 2017 - 20:53:12 - перейти к сообщению
CODE (htmlphp):
скопировать код в буфер обмена
  1.  
  2.  
  3. <?php
  4. $db = mysqli_connect("localhost", "root", "", "world") or die("Нет соединения с БД");
  5. mysqli_set_charset($db, "utf8") or die("Не установлена кодировка соединения");
  6.  
  7. function getCountries(){
  8.         global $db;
  9.         $query = "SELECT Code, Name FROM country";
  10.         $res = mysqli_query($db, $query);
  11.         return mysqli_fetch_all($res, MYSQLI_ASSOC);
  12. }
  13.  
  14. function getCities(){
  15.         global $db;
  16.         $code = mysqli_real_escape_string($db, $_POST['code']);
  17.         $query = "SELECT ID, Name FROM city WHERE CountryCode = '$code'";
  18.         $res = mysqli_query($db, $query);
  19.         $data = '';
  20.         while($row = mysqli_fetch_assoc($res)){
  21.                 $data .= "<option value='{$row['ID']}'>{$row['Name']}</option>";
  22.         }
  23.         return $data;
  24. }
  25.  
  26. if(!empty($_POST['code'])){
  27.         echo getCities();
  28.         exit;
  29. }
  30.  
  31.  
  32. function getLang(){
  33.         global $db;
  34.         $code = mysqli_real_escape_string($db, $_POST['code']);
  35.         $query = "SELECT Language FROM countrylanguage WHERE CountryCode = '$code'";
  36.         $res = mysqli_query($db, $query);
  37.   return mysqli_fetch_all($res, MYSQLI_ASSOC);
  38. }
  39.  
  40. $countries = getCountries();
  41.  
  42. ?>
  43. <!DOCTYPE html>
  44. <html lang="en">
  45. <head>
  46.         <meta charset="utf-8">
  47.         <meta http-equiv="X-UA-Compatible" content="IE=edge">
  48.         <meta name="viewport" content="width=device-width, initial-scale=1">
  49.         <title>Зависимые списки</title>
  50.         <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
  51.         <link rel="stylesheet" href="style.css">
  52. <!--[if lt IE 9]>
  53. <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  54. <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  55. <![endif]-->
  56. </head>
  57. <body>
  58.  
  59.         <div class="container content">
  60. <form class="form-horizontal" method="post" id="form">
  61.         <div class="form-group">
  62.                 <label for="name" class="col-sm-2 control-label">Страна</label>
  63.                 <div class="col-sm-6">
  64.                         <select class="form-control" name="country" id="country">
  65.                                 <option disabled selected>Выберите страну</option>
  66.                                 <?php foreach($countries as $country): ?>
  67.                                 <option value="<?=$country['Code']?>"><?=$country['Name']?></option>
  68.                                 <?php endforeach; ?>
  69.                         </select>
  70.                 </div>
  71.         </div>
  72.         <div class="form-group city-select">
  73.                 <label for="name" class="col-sm-2 control-label">Город</label>
  74.                 <div class="col-sm-6">
  75.                         <select class="form-control" name="city" id="city">
  76.                         </select>
  77.                 </div>
  78.         </div>
  79.  
  80.  
  81. <div class="form-group lang-select">
  82.                 <label for="name" class="col-sm-2 control-label">Язык</label>
  83.                 <div class="col-sm-6">
  84.                         <select class="form-control" name="lang" id="lang">
  85.                         </select>
  86.                 </div>
  87.         </div>
  88.  
  89.  
  90.  
  91.         <div class="form-group">
  92.                 <div class="col-sm-offset-2 col-sm-6">
  93.                         <button type="submit" id="submit" class="btn btn-primary">Отправить</button>
  94.                         <div></div>
  95.                 </div>
  96.         </div>
  97. </form>
  98.         </div>
  99.  
  100.         <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  101.         <script src="bootstrap/js/bootstrap.min.js"></script>
  102.  
  103.         <script>
  104. $(function(){
  105.  
  106.         $('#country').change(function(){
  107.                 var code = $(this).val();
  108.                 $('#city').load('index.php', {code: code}, function(){
  109.                         $('.city-select').fadeIn('slow');
  110.                 });
  111.  
  112.         });
  113.  
  114. });
  115.  
  116.  
  117.  
  118. $(function(){
  119.         $('#city').change(function(){
  120.                 var code = $(this).val();
  121.                 $('#lang').load('index.php', {code: code}, function(){
  122.                         $('.lang-select').fadeIn('slow');
  123.                 });
  124.  
  125.         });
  126.  
  127. });
  128.         </script>
  129.  
  130. </body>
  131. </html>
  132.  
  133.  
  134.  
  135.  



Доброго времени суток, уважаемые помогите исправить код, страны и города я вывожу а вот с языком запара скорее всего проблемы в function getLang

 

Powered by ExBB FM 1.0 RC1