Codeigniter错误消息:未定义的变量:记录

时间:2017-07-25 01:26:35

标签: php mysql codeigniter

我是CI的新手。我使用MY_Controller.php作为主控制器。我试图在CodeIgniter视图中打印数据对。但是,我收到以下错误。我做错了什么?

错误

  

遇到PHP错误

     

块引用
  严重性:通知
  消息:未定义的变量:记录
  文件名:views / country.php
  行号:21
   回溯:

     

C:\ xampp \ htdocs \ db_student \ application \ modules \ Reference \ views \ country.php Line: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行:370功能:视图
  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 \ 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'); ?>

4 个答案:

答案 0 :(得分:0)

我认为这意味着该变量未传递给视图。

这里是控制器

$this->load->view('country', ['records'=>$records]);

你确定$记录中有什么东西吗?

$this->load->view('country', ['records'=>'foo']);

这会产生同样的错误吗?

答案 1 :(得分:0)

在您的视图文件(country.php)中,

更改(Line Number: 21)

<?php if(count($records) ): ?>

<?php if( isset($records) && count($records) ): ?>

答案 2 :(得分:0)

尝试加载

$this->load->model('Crudcountry');

$data['records'] = $this->Crudcountry->getAllRecords( $record_id );

$this->load->view('update_country', $data);

http://www.codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view

另外,当在视图上加载另一个视图而不是include('header.php')时使用

<?php $this->load->view('header');?>

http://www.codeigniter.com/user_guide/general/views.html#loading-multiple-views

答案 3 :(得分:0)

我认为你必须发送这样的数据

    $record['records'] = $this->Crudcountry->getAllRecords( $record_ids);
    $this->load->view('update_country', $record);

并在此视图中访问数据

 <?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; ?>