PHP-MySQL+CodeIgniter无法显示patient_category表数据求助
问题描述
我用CodeIgniter的视图、模型、控制器代码展示数据库中patient_category表的数据,但页面未显示任何内容,也没有错误提示,不清楚问题出在哪个环节,希望得到排查帮助。
视图页面代码
<section id="main-content"> <section class="wrapper site-min-height"> <!-- page start--> <section class="panel"> <header class="panel-heading"> <?php echo lang('patient_categorie'); ?> <div class="col-md-4 no-print pull-right"> <a data-toggle="modal" href="#myModal"> <div class="btn-group pull-right"> <button id="" class="btn green btn-xs"> <i class="fa fa-plus-circle"></i> <?php echo lang('crer_p_categorie'); ?> </button> </div> </a> </div> </header> <div class="panel-body"> <div class="adv-table editable-table "> <div class="space15"></div> <table class="table table-striped table-hover table-bordered" id="editable-sample"> <thead> <tr> <th> <?php echo lang('patient_categorie'); ?></th> <th> <?php echo lang('patient_categorie_description'); ?></th> <th> <?php echo lang('options'); ?></th> </tr> </thead> <tbody> <style> .img_url{ height:20px; width:20px; background-size: contain; max-height:20px; border-radius: 100px; } </style> <?php foreach ($categories as $category) { ?> <tr class=""> <td><?php echo $category->category; ?></td> <td> <?php echo $category->description; ?></td> <td> <button type="button" class="btn btn-info btn-xs btn_width editbutton" data-toggle="modal" data-id="<?php echo $category->id; ?>"><i class="fa fa-edit"> <?php echo lang('edit'); ?></i> </button> <a class="btn btn-info btn-xs btn_width delete_button" href="patient/deletePatientCategorie?id=<?php echo $category->id; ?>" onclick="return confirm('Etes-vous sûr de vouloir supprimer cet élément?');"><i class="fa fa-trash-o"> <?php echo lang('delete'); ?></i></a> </td> </tr> <?php } ?> </tbody> </table> </div> </div> </section>
模型获取数据函数
function getPatientCategory() { $this->db->where('hospital_id', $this->session->userdata('hospital_id')); $query = $this->db->get('patient_category'); return $query->result(); }
控制器代码
public function patientCategory() { if (!$this->ion_auth->logged_in()) { redirect('auth/login', 'refresh'); } $data['categories'] = $this->patient_model->getPatientCategory(); $data['settings'] = $this->settings_model->getSettings(); $this->load->view('home/dashboard', $data); // just the header file $this->load->view('femme_enceinte', $data); $this->load->view('home/footer'); // just the header file } public function addCategoryView() { $data['settings'] = $this->settings_model->getSettings(); $this->load->view('home/dashboard', $data); // just the header file $this->load->view('add_new_category'); $this->load->view('home/footer'); // just the header file } public function addNewCategory() { $id = $this->input->post('id'); $category = $this->input->post('category'); $description = $this->input->post('description'); $this->load->library('form_validation'); $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); // Validating Category Name Field $this->form_validation->set_rules('category', 'Category', 'trim|required|min_length[2]|max_length[100]|xss_clean'); // Validating Description Field $this->form_validation->set_rules('description', 'Description', 'trim|required|min_length[5]|max_length[100]|xss_clean'); if ($this->form_validation->run() == FALSE) { $data['settings'] = $this->settings_model->getSettings(); $this->load->view('home/dashboard', $data); // just the header file $this->load->view('add_new_category'); $this->load->view('home/footer'); // just the header file } else { $data = array(); $data = array('category' => $category, 'description' => $description ); if (empty($id)) { $this->patient_model->insertPatientCategory($data); $this->session->set_flashdata('feedback', 'Ajouté'); } else { $this->patient_model->updatePatientCategory($id, $data); $this->session->set_flashdata('feedback', 'Modifié'); } redirect('patient/patientCategory'); } } function edit_category() { $data = array(); $id = $this->input->get('id'); $data['patient'] = $this->patient_model->getPatientCategoryById($id); $data['settings'] = $this->settings_model->getSettings(); $this->load->view('home/dashboard', $data); // just the header file $this->load->view('add_new_category', $data); $this->load->view('home/footer'); // just the footer file } function editPatientCategoryByJason() { $id = $this->input->get('id'); $data['patientcategory'] = $this->patient_model->getPatientCategoryById($id); echo json_encode($data); } function deletePatientCategory() { $id = $this->input->get('id'); $this->patient_model->deletePatientCategory($id); $this->session->set_flashdata('feedback', 'Supprimé'); redirect('patient/patientCategory'); }
排查步骤
- 开启错误提示:在项目根目录的
index.php里,把ENVIRONMENT常量改成'development',同时确保display_errors开启,这样能显示隐藏的PHP或数据库错误。 - 验证数据是否获取成功:在控制器的
patientCategory方法里,$data['categories'] = ...之后加一行var_dump($data['categories']); die;,查看是否有数据返回。如果为空:- 检查数据库
patient_category表中是否存在对应hospital_id的记录 - 用
var_dump($this->session->userdata('hospital_id'));验证会话中是否存在有效的hospital_id - 核对模型方法里的表名、字段名是否拼写正确
- 检查数据库
- 确认视图加载正确性:检查控制器中加载的
femme_enceinte视图是否就是你提供的展示分类的视图,有没有加载错误的文件。 - 检查视图变量是否存在:在视图的
foreach循环前加var_dump($categories);,确认变量是否成功传递到视图,有没有拼写错误(比如控制器里是categories,视图里写成了其他名字)。 - 调试数据库查询:在模型的
getPatientCategory方法里,return之前加echo $this->db->last_query(); die;,把生成的SQL语句复制到数据库管理工具中执行,看是否能得到结果。 - 检查登录状态:确认用户已正常登录,
ion_auth->logged_in()是否返回true,会不会因为未登录直接跳转但没有提示。
内容的提问来源于stack exchange,提问作者Denis yele
相关产品推荐
相关产品推荐

