a dynamic list with CI

i'm trying to create a multilevel list using codeigniter. the list would look like:

State1

city1
city2

state2

 city3

etc.

but when i run the code i get the error

            Severity: Notice

            Message: Undefined property: stdClass::$city

           Filename: views/testview.php

         Line Number: 32

at line 32 i have:

         <?php  foreach($state->city as $city):?>

my controller:

        //....

   $data['states'] = $this->state_model->get_cities();
   $this->load->view('testview', $data);

my model:

   function get_cities(){

  $states = $this->db->get('state');

  foreach ($states->result() as $state){
$cities = $this->db->get_where('city', array('state_id'=>$state->id));
$state->cities = $cities->result();
}

 return $states;

}

View:

   <?php foreach($states as $state):?>
 <h4><?php echo $state->statename;?></h4>
 <?php  foreach($state->city as $city):?>
<?php echo $city->cityname; ?>
 <?php endforeach;?>
<?php endforeach;?>

Answers


Change line 32 to

<?php foreach($state->cities as $city): ?>

You are looking for the cities property. In your model, the city results are assigned to

$state->cities in the $states array.


Need Your Help

SQL - left join generate duplicates

sql duplicates left-join

I have code to select some applications but LEFT JOIN is creating duplicates.

Building OutsystemsNow for android on Android Studio getting dexException

android android-gradle outsystems

I've imported the Outsystems Now android app into Android Studio. After several hours of trying to import the project and resolve all module imports. I'm currently getting this error with I can't f...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.