[eluser]draconus[/eluser]
I am following the tutorial located here:
http://codeigniter.com/wiki/Add_Edit_Views
But of course I am modifying it to my app and I am having some trouble getting it to work. The current error is:
Severity: Notice
Message: Undefined property: People::$person
Filename: controllers/people.php
Line Number: 25
Here is my model code for this portion:
Code:
function Get($id = NULL)
{
$this->db->select('*');
$this->db->from('people');
// Check if we're getting one row or all records
if($id != NULL)
{
// Getting only ONE row
$this->db->where('person_id', $id);
$this->db->limit('1');
$query = $this->db->get();
if( $query->num_rows() == 1 )
{
// One row, match!
return $query->row();
} else {
// None
return false;
}
} else {
// Get all
$query = $this->db->get();
if($query->num_rows() > 0)
{
// Got some rows, return as assoc array
return $query->result();
} else {
// No rows
return false;
}
}
}
function Add($data)
{
// Run query to insert blank row
$this->db->insert('people', array('person_id' => NULL) );
// Get id of inserted record
$id = $this->db->insert_id();
// Now call the edit function to update the actual data for this new row now we have the ID
return $this->Edit($id, $data);
}
function Edit($id, $data){
$this->db->where('person_id', $id);
$result = $this->db->update('people', $data);
// Return
if($result)
{
return $id;
} else {
return false;
}
}
Here is my view code:
Code:
<div id="content">
<h1>Add Person</h1>
<?php
if(!isset($person_id))
{
$person_id = @field($this->uri->segment(3, NULL), $this->validation->person_id, 'X');
}
echo form_open(
'person/save',
array('class' => 'cssform', 'id' => 'person_add'),
array('person_id' => $person_id)
);
?>
<fieldset><legend accesskey="D" tabindex="1">Person Information</legend>
<p>
<label for="first_name" class="required">first_name</label>
<?php
$first_name = @field($this->validation->first_name, $person->first_name);
echo form_input(array(
'name' => 'first_name',
'id' => 'name',
'size' => '20',
'maxlength' => '50',
'value' => $first_name,
));
?>
</p>
<?php echo @field($this->validation->first_name_error) ?>
<p>
<label for="last_name" class="required">last_name</label>
<?php
$last_name = @field($this->validation->last_name, $person->last_name);
echo form_input(array(
'name' => 'last_name',
'id' => 'name',
'size' => '20',
'maxlength' => '50',
'value' => $last_name,
));
?>
</p>
<?php echo @field($this->validation->last_name_error) ?>
<p>
<label for="company" class="required">company</label>
<?php
$company = @field($this->validation->company, $person->company);
echo form_input(array(
'name' => 'company',
'id' => 'name',
'size' => '20',
'maxlength' => '50',
'value' => $company,
));
?>
</p>
<?php echo @field($this->validation->company_error) ?>
<p>
<label for="phone" class="required">phone</label>
<?php
$phone = @field($this->validation->phone, $person->phone);
echo form_input(array(
'name' => 'phone',
'id' => 'name',
'size' => '20',
'maxlength' => '50',
'value' => $phone,
));
?>
</p>
<?php echo @field($this->validation->phone_error) ?>
</fieldset>
<div class="submit">
<?php echo form_submit(array('value' => 'Save')) ?>
<?php echo anchor('people', 'Cancel') ?>
</div>
</div>
I will post the controller code in a second post in this string. I am confident that once I figure out how to make this code work that I will have all the tools I need to complete most of my application since the code will be modifiable to meet my needs, and I can read most of it and understand what it does. The issue lies in the edit function of the controller.
Another subquestion here is the bottom of the tutorial which says to load the field helper but it doesn't specify where in the controller to load it.
Thanks in advance for the assistance in figuring out what the heck I did wrong when I modified the tutorial code.