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

时间:2022-12-04 16:33:54

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.

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

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

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

The code is as below:

代码如下:

Controller: (user_data.php)

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

型号(centers_model.php):

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

Model (group_model.php):

型号(group_model.php):

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

View (add_user.php)

查看(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 个解决方案

#1


1  

You get a blank page because there's a 500 internal error (most likely DB related issues). Check your php logs for errors, make sure that the table actually exists.

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

PS: You put the wrong filenames for each code. the model filenames look switched.

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

#2


0  

Thanks, I have solved the problem.

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

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

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

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

添加了$ this-> load-> model('centers_model','',TRUE);并且代码工作正常。

#1


1  

You get a blank page because there's a 500 internal error (most likely DB related issues). Check your php logs for errors, make sure that the table actually exists.

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

PS: You put the wrong filenames for each code. the model filenames look switched.

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

#2


0  

Thanks, I have solved the problem.

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

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

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

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

添加了$ this-> load-> model('centers_model','',TRUE);并且代码工作正常。