dou7466 2014-07-10 13:38 采纳率: 0%
浏览 82
已采纳

Codeigniter和JOIN查询有问题

All I am trying to do is query a database table for a list of counties in my state. I want to pull 5 cities that correspond to those counties and list them on the page. I am using a JOIN in order to do this because that is what my searching lead me to believe that I need.

So far I am passing all of the information from my Model to my Controller, and then displaying it in my View with no issues. The problem is that the cities list seems to be limiting its self to only 1 city per county. Do you see a problem with my query? I have tried (seemingly) everything, but because I am so new I am sure that there is something silly that I am missing.

<? 
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();
    }

}

This is the result of that query:

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" 
} 
}

It seems fine but for example, array[2] should have more cities listed under it, other than "Livonia." I can't seem to figure out how to complete this? Anyone have any advice or help?

  • 写回答

2条回答 默认 最新

  • duanjiang7505 2014-07-11 10:55
    关注

    I got it. I ended up getting rid of the JOIN because the array it was giving me was a 1:1 ratio for county to city. I needed it combined. So I queries the counties table and looped through that. As I looped through that I did loop through the cities data and checked to see if the county_id in the cities table matched the id in the counties table.

    Model:

    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();
    }
    

    Controller:

        $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');
    

    View:

    <?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>";
            }
        }
    }
    ?>
    

    From here I can style it and organize it on the page. Thank you so much for the help everybody. I hope this can help someone else with my problem. Also if anybody has a more efficient way of doing this I'd be happy to hear it!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统