I am a newbie in CI. I used MY_Controller.php as main controller. i'm trying to print pairs of data in my CodeIgniter view. However, I'm getting the following error. What I'm I doing wrong?
Error
A PHP Error was encountered
Blockquote
Severity: Notice
Message: Undefined variable: records
Filename: views/country.php
Line Number: 21
Backtrace:C:\xampp\htdocs\db_student\application\modules\Reference\views\country.php Line: 21 Function: _error_handler
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 Line: 304 Function: _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 Line: 304 Function: _ci_load
C:\xampp\htdocs\db_student\application\modules\Template\controllers\Template.php Line: 17 Function: view
C:\xampp\htdocs\db_student\application\modules\Reference\controllers\Reference.php Line: 21 Function: admin_template
File: C:\xampp\htdocs\db_student\index.php Line: 315 Function: require_once
My Controller
<?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');
}
}
?>
My Model
<?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));
}
}
?>
My View 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'); ?>
My View 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'); ?>
My View 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'); ?>