如何在codeigniter中显示嵌套数组(country - state - city hierarchy)

时间:2015-03-25 07:56:51

标签: php mysql arrays codeigniter

我想显示我所拥有的国家,州和城市列表的嵌套数组:

在第一个数组中,我需要country idcountry namestate id(状态ID在另一个数组中)

第二个是state idstate namecity id(城市ID是另一个数组)

SQL查询将使用什么?
有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

将在示例的帮助下解释(因为您还没有提供任何来自您的内容)

数据库:(3个表格)

1) `world_countries`

id  |   name
 1  |   Algeria
 2  |   Albania
80  |   India
266 |   United States of America
280 |   Zimbabwe 
_______________________________________________

2) `world_states`

id  |   name            | country_id
 1  |   Andhra Pradesh  |     80
 2  |   Assam           |     80
28  |   Uttarkhand      |     80
29  |   Uttarpradesh    |     80
150 |   California      |     266 
170 |   New York        |     266

_______________________________________________

3) `world_cities`

id  |   name            |  state_id
28  |   California      |     150
61  |   Guwahati        |     2
85  |   Hyderabad       |     1
122 |   New York        |     170


型号:(models / world_model.php)

<?php
class World_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->db = $this->load->database('default',true);
    }

    public function get_all_countries()
    {
        $query = $this->db->get('world_countries');
        return $query->result_array();
    }

    public function get_countries($where)
    {
        $query = $this->db->get_where('world_countries', $where);
        return $query->result_array();
    }

    public function get_all_states()
    {
        $query = $this->db->get('world_states');
        return $query->result_array();
    }

    public function get_states($where)
    {
        $query = $this->db->get_where('world_states', $where);
        return $query->result_array();
    }

    public function get_all_cites()
    {
        $query = $this->db->get('world_cities');
        return $query->result_array();
    }

    public function get_cities($where)
    {
        $query = $this->db->get_where('world_cities', $where);
        return $query->result_array();
    }
}


控制器:(controllers / xyz.php)

<?php
class Xyz extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('world_model');
    }

    public function index()
    {
        $countries = $this->world_model->get_all_countries();
        $i = 0;
        foreach($countries as $c)
        {
            $data['c_s'][$i]['country_id'] = $c['id'];
            $data['c_s'][$i]['country_name'] = $c['name'];

            $states = $this->world_model->get_states(array('country_id' => $c['id']));
            foreach($states as $s)
                $data['c_s'][$i]['state_id'][] = $s['id'];

            $i++;
        }

        $states = $this->world_model->get_all_states();
        $i = 0;
        foreach($states as $s)
        {
            $data['s_c'][$i]['state_id'] = $s['id'];
            $data['s_c'][$i]['state_name'] = $s['name'];

            $cities = $this->world_model->get_states(array('state_id' => $s['id']));
            foreach($cities as $c)
                $data['s_c'][$i]['city_id'][] = $c['id'];

            $i++;
        }

        $this->load->view('xyz', $data);
    }
}


查看:(views / xyz.php)

<table>
   <tr>
       <td>Country ID</td>
       <td>Country Name</td>
       <td>State ID</td>
   </tr>

   <?php foreach($c_s as $p1) { foreach($p1['state_id'] as $p2) { ?>
   <tr>
       <td><?php echo $p1['country_id']; ?></td>
       <td><?php echo $p1['country_name']; ?></td>
       <td><?php echo $p2['state_id']; ?></td>
   </tr>
   <?php } } ?>
</table>

<table>
   <tr>
       <td>State ID</td>
       <td>State Name</td>
       <td>City ID</td>
   </tr>

   <?php foreach($s_c as $p1) { foreach($p1['city_id'] as $p2) { ?>
   <tr>
       <td><?php echo $p1['state_id']; ?></td>
       <td><?php echo $p1['state_name']; ?></td>
       <td><?php echo $p2['city_id']; ?></td>
   </tr>
   <?php } } ?>    
</table>