[eluser]Sarfaraz Momin[/eluser]
Cannot say that this is the best or the most optimized way of doing it but it gets the job done. I m providing the view and controller code for your reference
view.php
Code:
<?
$this->load->helper('form');
$this->load->helper('url');
?>
<?
if($action == "add"){
?>
<form name="addeditform" id="addeditform" method="post" action="<?= base_url()?>admin/category/add/save/">
<table cellpadding="0" cellspacing="0" border="0" width="98%">
<tr><td colspan="2" align="center"><h3>Add New Category</h3></td></tr>
<?
}
elseif($action == "edit"){
?>
<form name="addeditform" id="addeditform" method="post" action="<?= base_url()?>admin/category/edit/save/">
<table cellpadding="0" cellspacing="0" border="0" width="98%">
<tr><td colspan="2" align="center"><h3>Update Category</h3></td></tr>
<?
}
?>
<tr><td colspan="2" align="center"><b><div id="msg"></div></b></td></tr>
<tr><td align="right" valign="top" width="50%">category name :</td><td><input name="category_name" value="<? echo ($action == "edit")? $query[0]->category_name:"";?>" style="width:200px;"></td></tr>
<tr style="padding-top:10px"><td align="right" valign="top" width="50%">metatags :</td><td><textarea name="metatags" style="width:250px;height:5em;"><? echo ($action == "edit")? $query[0]->metatags:"";?></textarea></td></tr>
<tr style="padding-top:10px"><td align="right" valign="top" width="50%">template :</td>
<td>
<select name="template" id="template">
<?
foreach($template as $tempitem):
echo "<option value=\"".$tempitem->template_id."\" ";
if($action == "edit"){
echo ($query[0]->template_id == $tempitem->template_id)? " selected":"";
}
echo ">".$tempitem->template_name."</option>";
endforeach;
?>
</select>
<input type="hidden" name="id" id="id" value="<? echo ($action == "edit")? $query[0]->category_id:"add";?>">
</td></tr>
<tr><td colspan="2" align="center" style="padding-top:10px">
<input type="submit" name="submit" value="Save">
</td></tr>
</table>
</form>
controller.php
Code:
if(!$this->uri->segment(3)){
$data['query'] = $this->AdminModel->get_category();
$this->load->view('adminviews/adminview_category', $data);
}
if($this->uri->segment(3) == "add"){ //add new category
if($this->uri->segment(4) == "save"){
$this->load->database();
$data = array(
'category_name' => $_POST["category_name"],
'metatags' => $_POST["metatags"] ,
'template_id' => $_POST["template"]
);
$this->db->insert('category', $data);
}
if($this->uri->segment(4) <> "save"){
$data['template'] = $this->AdminModel->get_template();
$data['action'] = "add";
$this->load->view("adminviews/adminview_category_addedit",$data);
}
}
if($this->uri->segment(3) == "edit"){ //update category
if($this->uri->segment(4) == "save"){
$this->load->database();
$data = array(
'category_name' => $_POST["category_name"],
'metatags' => $_POST["metatags"] ,
'template_id' => $_POST["template"]
);
$this->db->where('category_id', $_POST["id"]);
$this->db->update('category', $data);
}
if($this->uri->segment(4) <> "save"){
$data['query'] = $this->AdminModel->get_one_category($this->uri->segment(4));
$data['template'] = $this->AdminModel->get_template();
$data['action'] = "edit";
$this->load->view("adminviews/adminview_category_addedit",$data);
}
}