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

时间:2017-09-09 07:30:56

标签: php mysql codeigniter

编辑:[已解决] - 未加载模型。

我可以显示从表中提取的组列表​​。但无法显示中心列表。

我无法找到发生错误的地方。

当我取消注释$data['allcenters']时,我只是得到一个空页面。但是当我删除该行时,页面显示正常但没有中心列表。

组的表名是user_groups。 中心的表名是institute_data。

代码如下:

控制器:(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);
}

模型(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;
    }

查看(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 个答案:

答案 0 :(得分:1)

你得到一个空白页面,因为有500个内部错误(很可能是数据库相关的问题)。检查您的php日志中的错误,确保该表实际存在。

PS:你为每个代码添加了错误的文件名。模型文件名看起来切换。

答案 1 :(得分:0)

谢谢,我已经解决了这个问题。

我没有在__construct()函数中加载模型。

添加了$this->load->model('centers_model','',TRUE);,代码运行正常。