我是CI的新手。我使用MY_Controller.php作爲主控制器。我試圖在CodeIgniter視圖中打印數據對。但是,我收到以下錯誤。我做錯了什麼?Codeigniter錯誤消息:未定義的變量:記錄
錯誤
遇到一個PHP錯誤
塊引用
嚴重性:注意
消息:未定義的變量:記錄
文件名:觀點/ country.php
行號:21
Backtrace:C:\ xampp \ htdocs \ db_student \ application \ modules \ Reference \ views \ country.php行:21函數:_error_handler
C:\ xampp \ htdocs \ db_student \ application \ third_party \ MX \ Loader.php行:362功能:包括
C:\ xampp \ htdocs \ db_student \ application \ third_party \ MX \ Loader.php行:304函數:_ci_load
C:\ xampp \ htdocs \ db_student \ application \ modules \ Template \ views \ v_admin_template。 php Line:370 Function:view
C:\ xampp \ htdocs \ db_student \ application \ third_party \ MX \ Loader.php Line:362 Function:include
C:\ xampp \ htdocs \ db_student \ application \ third_party \ MX \ Loader.php行:304功能:_ci_load
C:\ xampp \ htdocs \ d b_student \ application \ modules \ Template \ controllers \ Template.php行:17功能:查看
C:\ xampp \ htdocs \ db_student \ application \ modules \ Reference \ controllers \ Reference.php行:21功能:admin_template
文件:C:\ XAMPP \ htdocs中\ db_student \ index.php的線:315功能:require_once
我的控制器
<?php
class C_country extends MY_Controller{
public function index() {
$this->load->model('Crudcountry');
$records = $this->Crudcountry->getRecords();
$this->load->view('country', ['records'=>$records]);
}
public function create(){
$this->load->view('create_country');
}
public function save(){
$this->form_validation->set_rules('country_name', 'Country Name', 'required');
$this->form_validation->set_error_delimiters('<div class="text-danger">', '</div>');
if($this->form_validation->run())
{
$data = $this->input->post();
$this->load->model('Crudcountry');
if($this->Crudcountry->saveRecord($data)){
$this->session->set_flashdata('response', 'Record Saved Successfully.');
}
else{
$this->session->set_flashdata('response', 'Record Failed to Save!');
}
return redirect('C_country');
}
else
{
$this->load->view('create_country');
}
}
public function edit($record_id){
$this->load->model('Crudcountry');
$record = $this->Crudcountry->getAllRecords($record_id);
$this->load->view('update_country', ['record'=>$record]);
}
public function update($record_id){
$this->form_validation->set_rules('country_name', 'Country Name', 'required');
$this->form_validation->set_error_delimiters('<div class="text-danger">', '</div>');
if($this->form_validation->run())
{
$data = $this->input->post();
$this->load->model('Crudcountry');
if($this->Crudcountry->updateRecords($record_id, $data)){
$this->session->set_flashdata('response', 'Record Updated Successfully.');
}
else{
$this->session->set_flashdata('response', 'Failed to Update!');
}
return redirect('C_country');
}
else
{
$this->load->view('update');
}
}
public function delete($record_id){
$this->load->model('Crudcountry');
if($this->Crudcountry->deleteRecord($record_id)){
$this->session->set_flashdata('response', 'Record Deleted Successfully.');
}
else{
$this->session->set_flashdata('response', 'Failed to Delete Record!.');
}
return redirect('C_country');
}
}
?>
我的模型
<?php
class Crudcountry extends CI_Model{
public function getRecords(){
$query = $this->db->get('refcountry');
return $query->result();
}
public function saveRecord($data){
return $this->db->insert('refcountry', $data);
}
public function getAllRecords($record_id){
$query = $this->db->get_where('refcountry', array('id_country' => $record_id));
if($query->num_rows() > 0){
return $query->row();
}
}
public function updateRecords($record_id, $data){
return $this->db->where('id_country', $record_id)
->update('refcountry', $data);
}
public function deleteRecord($record_id){
return $this->db->delete('refcountry', array('id_country' => $record_id));
}
}
?>
我查看country.php
<?php include('header.php'); ?>
<div class="container">
<?php if($error = $this->session->flashdata('response')): ?>
<div class="alert alert-dismissible alert-success">
<?php echo $error; ?>
</div>
<?php endif; ?>
<div class="row">
<div class="col-lg-12">
<?php echo anchor("Reference/C_country/create", 'Create', ['class'=>'btn btn-primary']); ?>
</div>
</div>
<table class="table table-striped table-hover ">
<thead>
<tr>
<th>Country Name</th>
<th>Operations</th>
</tr>
</thead>
<tbody>
<?php if(count($records)): ?>
<?php foreach($records as $record): ?>
<tr>
<td><?php echo $record->country_name; ?></td>
<td><?php echo anchor("C_country/edit/{$record->id_country}", 'Update', ['class'=>'btn btn-success']); ?></td>
<td><?php echo anchor("C_country/delete/{$record->id_country}", 'Delete', ['class'=>'btn btn-danger']); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>No Records Found!</tr>
<?php endif; ?>
</tbody>
</table>
</div>
<?php include('footer.php'); ?>
我查看create_country.php
<?php include('header.php'); ?>
<div class="container">
<?php echo form_open('Reference/C_country/save', ['class'=>'form-horizontal']); ?>
<fieldset>
<div class="container"> </div>
<legend>Create Country</legend>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="inputEmail" class="col-lg-4 control-label">Country Name</label>
<div class="col-lg-8">
<?php echo form_input(['name'=>'country_name', 'class'=>'form-control', 'placeholder'=>'Country Name', 'value'=>set_value('country_name')]); ?>
</div>
</div>
</div>
<div class="col-lg-6">
<?php echo form_error('country_name'); ?>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<?php echo form_submit(['value'=>'Submit', 'class'=>'btn btn-primary']); ?>
<?php echo form_reset(['value'=>'Reset', 'class'=>'btn btn-default']); ?>
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
</div>
<?php include('footer.php'); ?>
我查看update_country.php
<?php include('header.php'); ?>
<div class="container">
<?php echo form_open("Reference/C_country/update/{$record->id_country}", ['class'=>'form-horizontal']); ?>
<fieldset>
<div class="container"> </div>
<legend>Create Country</legend>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="inputEmail" class="col-lg-4 control-label">Country Name</label>
<div class="col-lg-8">
<?php echo form_input(['name'=>'country_name', 'class'=>'form-control', 'placeholder'=>'Country Name', 'value'=>set_value('country_name', $record->country_name)]); ?>
</div>
</div>
</div>
<div class="col-lg-6">
<?php echo form_error('country_name'); ?>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<?php echo form_submit(['value'=>'Submit', 'class'=>'btn btn-primary']); ?>
<?php echo form_reset(['value'=>'Reset', 'class'=>'btn btn-default']); ?>
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
</div>
<?php include('footer.php'); ?>
請嘗試減少您的代碼,以便問題仍然存在。扔掉(註釋掉)不同的部分來跟蹤究竟導致問題的原因:無論如何,這既是你需要發展的技能,也是增加你的問題得到解答的機會。此致敬禮 – YakovL
請閱讀如何創建[mcve]。這裏發佈的代碼太多了。嘗試用小塊代碼縮小您遇到的問題。 –
減少BlockQuote大小,刪除不相關的Apache標籤 –