doufuxi7093 2011-11-08 17:50
浏览 34
已采纳

带有codeigniter的多级菜单

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条回答 默认 最新

  • dormbaf90464 2011-11-08 18:29
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败