2015-03-25 2 views
-1

Я хочу, чтобы отобразить вложенный массив списка я имею в стране, штат и город:Как отобразить вложенный массив в CodeIgniter (страна - государство - город иерархия)

В первом массиве мне нужно country id, country name и state id (идентификатор состояния находится в другом массиве)

Второй является state id, state name, city id (город идентификатор другой массив)

Что SQL-запрос будет идти с этим?
Может ли кто-нибудь помочь?

+0

пожалуйста, покажите соответствующий вывод, который вы предпочитаете –

+1

, что вы пробовали до настоящего времени? – Sundar

+0

@NarendraSisodia Я хочу отобразить массив следующим образом: Array [3] – Geetika

ответ

0

Поясню с помощью примера (так как вы не предоставили никакой контент из yourside)

База данных: (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 


Модель: (модели/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(); 
    } 
} 


Контроллер: (контроллеры/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); 
    } 
} 


Вид: (просмотров/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> 
+1

Обратите внимание, что '$ query-> result()' будет возвращать массив объектов, поэтому вам понадобится '$ query-> result_array()' вместо – Gervs

+0

@Gervs: Хорошая добыча !! Для простоты я сделаю все «массив» в этом случае. Спасибо :) –

+0

@parag Tyagi Я попробовал ваш код, но столкнулся с ошибкой «Использование неопределенной константы c_s - предполагаемый« c_s ».... Я новичок в codeigniter, пожалуйста, укажите, где я ошибаюсь – Geetika

Смежные вопросы