Creating foreach loops using Code Igniter controller and view

前端 未结 4 1718
一整个雨季
一整个雨季 2021-01-17 02:35

This is a situation I have found myself in a few times and I just want clear it up once and for all.

Best just to show you what I need to do in some example code.

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-17 02:49

    You should do a join in your model for both tables, and create one result which you will move later to your view trough the controller.

    Remember not to put queries in your controller.

    maybe somthing like this:

    function get_data($cue_sheets){
    $this->db->select(clips.*);
    $this->db->from('clips');
    $this->db->join('cue_sheets', 'clips.idcuesheet = cue_sheets.idcuesheet' );
    $this->db->where('cue_sheets.idcuesheet', $cue_sheets);
    $query = $this->db->get();
    }
    

    Or something similar.

    Then you bring it to the controller

    $data['clips'] = $this->clips->get_data($cue_sheets);
    

    and finally to your view

    foreach($clips as $clip){
    echo $clip->clip_name;
    }
    

    Hope it helps

提交回复
热议问题