The relevant bits of my code:
Model:
class Meta_model extends Model {
function Meta_model() {
parent::Model();
}
// _add_meta() function's here
function _get_all_meta() {
$this->db->select('id, mttitle')->from('meta_types')->orderby('mttitle', 'asc');
$query = $this->db->get();
return $query;
}
function _get_meta_id($edit) {
$this->db->select('id, mttitle, mttype, mtdesc, mtswitch')->from('meta_types')->where('id', $edit)->limit(1);
$query = $this->db->get();
return $query;
}
// _update_meta() function's here
}
The controller:
class Meta extends Controller {
function Meta() {
parent::Controller();
$this->load->model('Meta_model', 'Meta', TRUE);
}
function add() {
// add function's here
}
function edit() {
$this->load->helper('form');
$this->load->library('validation');
$edit = $this->uri->segment(3,0);
if ($edit == 0) {
$list = array();
$query = $this->Meta->_get_all_meta();
foreach ($query->result_array() as $row):
$list[$row['id']] = $row['mttitle'];
endforeach;
$data['list'] = $list;
$this->load->view('meta_list_all', $data);
} else {
$rules['id'] = "required|numeric";
$rules['title'] = "trim|required|max_length[255]|xss_clean";
$rules['type'] = "required";
$rules['desc'] = "trim|xss_clean";
$rules['trigger'] = "required";
$fields['id'] = 'id';
$fields['title'] = 'title';
$fields['type'] = 'type';
$fields['desc'] = 'description';
$fields['trigger'] = 'trigger';
$this->validation->set_fields($fields);
if (empty($_POST)) {
$query = $this->Meta->_get_meta_id($edit);
$default = array();
foreach ($query->result_array() as $row):
$default['id'] = $row['id'];
$default['title'] = $row['mttitle'];
$default['type'] = $row['mttype'];
$default['desc'] = $row['mtdesc'];
$default['trigger'] = $row['mtswitch'];
endforeach;
$this->validation->set_defaults($default);
}
$this->validation->set_error_delimiters('<br /><span class="error">', '</span>');
$this->validation->set_rules($rules);
if ($this->validation->run() == FALSE) {
$this->load->view('meta_form');
} else {
$id = $this->input->post('id');
$mttitle = $this->input->post('title');
$type = $this->input->post('type');
$desc = $this->input->post('desc');
$trigger = $this->input->post('trigger');
$data = array(
'id' => $id,
'mttitle' => $mttitle,
'mttype' => $type,
'mtdesc' => $desc,
'mtswitch' => $trigger
);
$this->Meta->_update_meta($data, $id);
$this->load->view('meta_success');
}
}
}
}
And the form view (meta_form):
<?
$data['title'] = ' :: Admin :: Meta Categories';
$this->load->view('header.php', $data);
?>
<body>
<h2 id="header">Meta Categories</h1>
<div id="content">
<?
$url = $this->uri->segment(2) . '/' .$this->uri->segment(3);
echo form_open('meta/'.$url);
if ($this->uri->segment(2) == 'edit'): ?>
<input type="hidden" name="id" value="<?=$this->validation->id;?>" />
<? endif; ?>
<table cellpadding="5">
<tr>
<td><label for="title">Meta category title:</label></td>
<td><input type="text" name="title" value="<?=form_prep($this->validation->title);?>" size="50" max="255" /> <?=$this->validation->title_error; ?></td>
</tr>
<tr>
<td><label for="type">How many subcategories may be selected at once?</label></td>
<td><input type="radio" name="type" value="0"<?=$this->validation->set_radio('type', '0');?> /> Only one <input type="radio" name="type" value="1"<?=$this->validation->set_radio('type', '1');?> /> More than one <?=$this->validation->type_error; ?></td>
</tr>
<tr>
<td><label for="desc">Description:</label></td>
<td><textarea name="desc" rows="10" cols="30"><?=form_prep($this->validation->desc);?></textarea><?=$this->validation->desc_error; ?></td>
</tr>
<tr>
<td><label for="trigger">Switch this meta category on or off:</label></td>
<td><input type="radio" name="trigger" value="1"<?=$this->validation->set_radio('trigger', '1');?> /> On <input type="radio" name="trigger" value="0"<?=$this->validation->set_radio('trigger', '0');?> /> Off <?=$this->validation->trigger_error; ?></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
<p><?=anchor('meta', 'Back to meta type index.');?></p>
</div>
<? $this->load->view('footer.php'); ?>
Thanks! :)