Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6777801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:11:28+00:00 2026-05-26T16:11:28+00:00

i’m trying to create a multilevel list. I have two tables ‘State’ and ‘City’.

  • 0

i’m trying to create a multilevel list. I have two tables ‘State’ and ‘City’. The city table has a foreign key ‘state_id’ which is the primary key of ‘State’ table. I want to show each state and under each state there is multiple cities. But when i run my code only the last stored state in the db and the cities under it show up. I want all the states in the Db and the cities corresponds to it to appear.

Part of my controller:

function index(){
    $result = $this->db->count_all('state');

    $id=1;
    while ($id<=$result){
        $data ['state'] = $this->state_model->stateid($id);  
        $data['city']=$this->state_model->statec($id);
        $id++;
    } 
    $this->load->view('state_view',$data);
}

The model:

function stateid($id = 0){
    $this->db->where('id',$id);

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

    return $sql->result();
}

function statec($id = 0){
    $this->db->where('state_id',$id);

    $sql = $this->db->get('city');

    return $sql->result();
}

The view:

<?php foreach($state as $row):?>
    <h4><?php echo $row->statename;?></h4>
    <?php foreach($city as $row):?>
        <?php echo $row->cityname; ?></br></br></br>
    <?php endforeach;?></br></br>
<?php endforeach;?></br></br>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:11:29+00:00Added an answer on May 26, 2026 at 4:11 pm

    That is because in that while, with every iteration you overwrite the $data[‘state’] and $data[‘city’]… so only the last iteration gets sent to the view.

    You should look more into what arrays and especially multi-dimensional arrays work and how to use them.

    That being said, you have bigger problems to consider. Like the code is very badly optimised and will cause problems later. Like your code assumes that the ids in the state table are consecutive, which is not necessarily true.

    Here is a rought ideea what your code should look like:

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

    controller

    function index(){
      $this->load->model('state_model);
      $data['states'] = $this->state_model->get_cities();
      $this->load->view('states/index, $data);
    }
    

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

    Good luck

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.