Codeigniter和JOIN查询有问题

时间:2014-07-10 13:38:30

标签: php mysql codeigniter foreign-key-relationship

我要做的就是在数据库表中查询我所在州的县列表。我想拉出与这些县相对应的5个城市,并在页面上列出。我正在使用JOIN来执行此操作,因为这是我的搜索引导我相信我需要的。

到目前为止,我将所有信息从我的模型传递到我的控制器,然后在我的视图中显示它,没有任何问题。问题是城市名单似乎将自己限制在每个县只有一个城市。你看到我的查询有问题吗?我曾经(似乎)尝试了一切,但因为我是如此新鲜,我确信我有一些愚蠢的东西,我不知道。

<? 
class Index_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->database();
    }

    public function countiesWithCities() {
        $this->db->select("*");
        $this->db->from("cities");
        $this->db->join("counties", "cities.county_id = counties.county_id");
        $query = $this->db->get();
        return $query->result();
    }

}

这是该查询的结果:

array(5) { 
[0]=> object(stdClass)#17 (5) { 
["city_id"]=> string(1) "0" 
["county_id"]=> string(1) "5" 
["city_name"]=> string(8) "Munising" 
["county_name"]=> string(5) "Alger" 
["number_of_cities"]=> string(1) "1" 
} 
[1]=> object(stdClass)#18 (5) { 
["city_id"]=> string(1) "1" 
["county_id"]=> string(1) "1" 
["city_name"]=> string(7) "Redford" 
["county_name"]=> string(5) "Wayne" 
["number_of_cities"]=> string(1) "2" 
} 
[2]=> object(stdClass)#19 (5) { 
["city_id"]=> string(1) "2" 
["county_id"]=> string(1) "1" 
["city_name"]=> string(7) "Livonia" 
["county_name"]=> string(5) "Wayne" 
["number_of_cities"]=> string(1) "2" 
} 
[3]=> object(stdClass)#20 (5) { 
["city_id"]=> string(1) "3" 
["county_id"]=> string(1) "4" 
["city_name"]=> string(6) "Monroe" 
["county_name"]=> string(6) "Monroe" 
["number_of_cities"]=> string(1) "1" 
} 
[4]=> object(stdClass)#21 (5) { 
["city_id"]=> string(1) "4" 
["county_id"]=> string(1) "2" 
["city_name"]=> string(16) "Farmington Hills" 
["county_name"]=> string(7) "Oakland" 
["number_of_cities"]=> string(1) "1" 
} 
}

看起来很好但是例如,数组[2]应该有更多的城市列在其下,除了&#34; Livonia。&#34;我似乎无法弄清楚如何完成这个?有人有任何建议或帮助吗?

2 个答案:

答案 0 :(得分:0)

您正在打印来自城市和国家/地区表的结果。请参阅选择表格行。 由于country表只有一行带有county_id,因此它只会打印一行。 控制器功能

public function counties()
{
    $this->db->select('*');
    $this->db->from('counties');
    $query = $this->db->get();
    return $query->result();
    }
public function Cities() {
    $this->db->select("cities.*");
    $this->db->from("cities");
    $this->db->join("counties", "cities.county_id = counties.county_id");
    $query = $this->db->get();
    return $query->result();
}

现在,在查看页面中,请为以下各项使用双倍。

<?php 
    foreach ($rs_countrytable as $rs_countrytable_value)  
    {

  echo $rs_countrytable_value->country_name;
?><br/>
<?php
        foreach ($rs_Cities as $rs_Cities_value)
        { 
        if(($rs_countrytable_value->county_id)==$rs_Cities_value->county_id)    
          {
          echo $rs_Cities_value->city_name;?><br/><?php
          }
        }
     }?>

答案 1 :(得分:0)

我明白了。我最终摆脱了JOIN,因为它给我的阵列是县与城市的1:1比例。我需要它结合起来。所以我查询了县表并循环了解。当我循环访问时,我确实循环遍历城市数据并检查以查看cities表中的county_id是否与县表中的id匹配。

型号:

public function countiesWithCities() {
    $this->db->select("*");
    $this->db->from("counties");
    $this->db->where('number_of_cities >', 0);
    $this->db->order_by("id", "asc");
    $query = $this->db->get();
    return $query->result();
}

public function getCities() {
    $this->db->select("*");
    $this->db->from("cities");
    $this->db->order_by("county_id", "asc");
    $query = $this->db->get();
    return $query->result();
}

控制器:

    $data['counties'] = $this->index_model->countiesWithCities();
    $data['cities'] = $this->index_model->getCities();


    //The page "home" does exist, so load the views in the following order
    $this->load->view('templates/header', $data);
    $this->load->view('templates/indexmain', $data);
    $this->load->view('templates/footer');

查看:

<?php 
foreach ($counties as $county)  {

    echo "<strong>".$county->county_name."</strong> <br>";

    foreach($cities as $city) {
        if($city->county_id == $county->id) {
            echo $city->city_name."<br>";
        }
    }
}
?>

从这里我可以设计它并在页面上组织它。非常感谢大家的帮助。我希望这可以帮助别人解决我的问题。此外,如果有人有更有效的方式这样做,我会很高兴听到它!