在循环中,错误的结果

时间:2015-06-28 12:43:59

标签: php mysql while-loop

我需要一个显示以下内容的脚本:Country>地区(很多)>这个地区的城市(也很多)。试试,但不能:(帮助

$get_all = $db->query("SELECT * FROM ".PREFIX."_shop_cities WHERE type='0'");   # Берем страны
    while($row = $db->get_row($get_all)){
        $get_regions = $db->query("SELECT * FROM ".PREFIX."_shop_cities WHERE god='$row[id]' AND type='1'"); # Регионы
        while($row2 = $db->get_row($get_regions)){
            $get_cities = $db->query("SELECT * FROM ".PREFIX."_shop_cities WHERE god='$row2[id]' AND type='2'"); # города
            while($row3 = $db->get_row($get_cities)){
                $tpl->load_template("eshop/citylist.tpl");
                $tpl->set("{city_name}", "<li>".$row3["name_ru"]."</li>");
                $tpl->compile("cities");
                $tpl->clear();
            }
            $tpl->load_template("eshop/regionslist.tpl");
            $tpl->set("{region_name}", $row2["name_ru"]);
                $tpl->set("{cities}", $tpl->result["cities"]);
            $tpl->compile("regions");
            $tpl->clear();
        }
        $tpl->load_template("eshop/countrylist.tpl");
        $tpl->set("{name_country}", $row["name_ru"]);
        $tpl->set("{regions}", $tpl->result["regions"]);
        $tpl->compile("citylist");
        $tpl->clear();
    }

2 个答案:

答案 0 :(得分:0)

我试着这样做:

$get_all = $db->query("SELECT a.name_ru as Country, b.name_ru AS Region, c.name_ru AS City FROM dle_shop_cities AS a JOIN dle_shop_cities AS b ON a.id = b.god AND b.type = 1 JOIN dle_shop_cities AS c ON b.id = c.god AND c.type = 2");   # Берем страны
while($row = $db->get_row($get_all)){

            $tpl->load_template("eshop/citylist.tpl");
            $tpl->set("{city_name}", "<li>".$row["City"]."</li>");
            $tpl->compile("cities");
            $tpl->clear();

        $tpl->load_template("eshop/regionslist.tpl");
        $tpl->set("{region_name}", $row[Region]);
            $tpl->set("{cities}", $tpl->result["cities"]);
        $tpl->compile("regions");
        $tpl->clear();

    $tpl->load_template("eshop/countrylist.tpl");
    $tpl->set("{name_country}", $row["Country"]);
    $tpl->set("{regions}", $tpl->result["regions"]);
    $tpl->compile("citylist");
    $tpl->clear();
}

不成功, Россия Новосибирскаяобласть Омск 的Россия Новосибирскаяобласть Омск Новосибирскаяобласть Омск Новосибирск 的Россия Новосибирскаяобласть Омск Новосибирскаяобласть Омск Новосибирск Московскаяобласть Омск Новосибирск Москва 请再次帮助

答案 1 :(得分:0)

试试这个

$country = $region = '';
$get_all = $db->query("SELECT a.name_ru as Country, b.name_ru AS Region, c.name_ru AS City FROM dle_shop_cities AS a JOIN dle_shop_cities AS b ON a.id = b.god AND b.type = 1 JOIN dle_shop_cities AS c ON b.id = c.god AND c.type = 2 ORDER BY a.name_ru, b.name_ru, c.name_ru");   # Берем страны
while($row = $db->get_row($get_all)) {
  if($country != $row['Country']) {
    $tpl->load_template("eshop/countrylist.tpl");
    $tpl->set("{name_country}", $row["Country"]);
    $tpl->compile("citylist");
    $tpl->clear();
    $country = $row['Country'];
  }
  if($region != $row['Region']) {
    $tpl->load_template("eshop/regionslist.tpl");
    $tpl->set("{region_name}", $row[Region]);
    $tpl->compile("regions");
    $tpl->clear();
    $region = $row['Region'];
  }
  $tpl->load_template("eshop/citylist.tpl");
  $tpl->set("{city_name}", "<li>".$row["City"]."</li>");
  $tpl->compile("cities");
  $tpl->clear();
}