dsbqfrr098575666 2017-09-09 07:30
浏览 20
已采纳

无法使用codeigniter列出另一个表数据

Edit: [Solved] - Did not load the model.

I'm able to display the list of groups fetched from the table. But unable to display the list of centers.

I'm unable to find where I have made an error.

When I uncomment the $data['allcenters'], I just get an empty page. But when I remove the line, the page displays fine but without the list of centers.

The table name of groups is user_groups. The table name of centers is institute_data.

The code is as below:

Controller: (user_data.php)

function add_new(){
$logged_in=$this->session->userdata('logged_in');
if($logged_in['su']!="1"){
exit('Permission denied');
return; 
}       
    $data['title']="Add User";

    //get the list of the groups
    $data['allgroups'] = $this->group_model->get_allgroups();

    //get the list of all centers
    $data['allcenters'] = $this->centers_model->get_allcenters();
    //Page is displayed when the above code is commented. 
    //On inserting the above line, the page is just blank

    $this->load->view($this->session->userdata('web_view').'/header',$data);
    $this->load->view($this->session->userdata('web_view').'/add_user',$data);
    $this->load->view($this->session->userdata('web_view').'/footer',$data);
}

Model (centers_model.php):

function get_allcenters(){
    $query = $this->db->get("institute_data");
    $result = $query->result_array();
    return $result;
    }

Model (group_model.php):

function get_allgroups(){
    $query = $this->db->get("user_group");
    $result = $query->result_array();
    return $result;
    }

View (add_user.php)

<?php print_r($allcenters); ?>
<div class="form-group">
    <label>Center </label>
    <select class="form-control" name="centers">
    <?php foreach($allcenters as $key => $center){ ?>
    <option value="<?php echo $center['su_institute_id']; ?>"><?php echo $center['organization_name']; ?></option>
    <?php } ?>
    </select>
 </div>

<?php print_r($allgroups); ?>
<div class="form-group">
    <label>Group </label>
    <select class="form-control" name="user_group">
    <?php foreach($allgroups as $key => $group){ ?>
    <option value="<?php echo $group['gid']; ?>"><?php echo $group['group_name']; ?></option>
    <?php } ?>
    </select>
 </div>
  • 写回答

2条回答 默认 最新

  • douduoting8408 2017-09-09 09:47
    关注

    Thanks, I have solved the problem.

    I have not loaded the model in the __construct() function.

    Added $this->load->model('centers_model','',TRUE); and the code works fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改