Php 如何在codeigniter(国家-州-城市层次结构)中显示嵌套数组
我想显示国家、州和城市列表的嵌套数组: 在第一个数组中,我需要Php 如何在codeigniter(国家-州-城市层次结构)中显示嵌套数组,php,mysql,arrays,codeigniter,Php,Mysql,Arrays,Codeigniter,我想显示国家、州和城市列表的嵌套数组: 在第一个数组中,我需要国家id、国家名称和州id(州id在另一个数组中) 第二个是州id,州名,城市id(城市id是另一个数组) 什么样的SQL查询将与此匹配? 有人能帮忙吗?将通过一个例子来解释(因为您没有提供任何来自您这边的内容) 数据库:(3个表) 模型:(models/world\u Model.php) 州ID 州名 城市标识 请显示您目前尝试的输出?@NarendraSisodia我想这样显示数组:数组[3]注意$query->result(
国家id
、国家名称
和州id
(州id在另一个数组中)
第二个是州id
,州名
,城市id
(城市id是另一个数组)
什么样的SQL查询将与此匹配?有人能帮忙吗?将通过一个例子来解释(因为您没有提供任何来自您这边的内容) 数据库:(3个表)
模型:(models/world\u Model.php)
州ID
州名
城市标识
请显示您目前尝试的输出?@NarendraSisodia我想这样显示数组:数组[3]注意$query->result()
将返回一个对象数组,因此您需要$query->result\u array()
instead@Gervs:接得好!!为了简单起见,在这种情况下,我将使一切都成为数组。谢谢:)@parag Tyagi我尝试了你的代码,但是遇到了“使用未定义的常数c_s-假定的c_s”的错误……我是codeigniter的新手,请指导我在哪里wrong@Anonymus:我的错。我没有添加引号(它应该像'c\u s'
而不是c\u s
)。请检查更新的ans。@ParagTyagi感谢您的帮助,现在我收到了这个错误:“尝试获取非对象的属性”
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
<?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();
}
}
<?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);
}
}
<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>