简体   繁体   English

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

[英]Unable to list the another table data using 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. 当取消注释$data['allcenters'] ,我只会得到一个空白页。 But when I remove the line, the page displays fine but without the list of centers. 但是,当我删除该行时,页面显示正常,但没有中心列表。

The table name of groups is user_groups. 组的表名是user_groups。 The table name of centers is institute_data. 中心的表名称是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>

You get a blank page because there's a 500 internal error (most likely DB related issues). 您会得到一个空白页,因为存在500个内部错误(很可能是与数据库有关的问题)。 Check your php logs for errors, make sure that the table actually exists. 检查您的php日志中是否有错误,请确保该表确实存在。

PS: You put the wrong filenames for each code. PS:您为每个代码输入了错误的文件名。 the model filenames look switched. 模型文件名看起来切换了。

Thanks, I have solved the problem. 谢谢,我已经解决了问题。

I have not loaded the model in the __construct() function. 我尚未在__construct()函数中加载模型。

Added $this->load->model('centers_model','',TRUE); 添加了$this->load->model('centers_model','',TRUE); and the code works fine. 和代码工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM