[eluser]n0xie[/eluser]
I don't know if this is the 'right' way but for simple forms I usually do it like this:
Simple example:
You have a database which holds an id field, and a catagoryId. I assume the id field is a primary key which has an autoincrement on it. So it's the identifying key. If we want to add a new entry, then this id will be 0 in our view. Else our id will be our primary key value.
To explain what's going on:
In the
add function I just pull up data that's Necessary to render the form. Then I pass it on the renderform private method which 'renders the form'.
If I want to edit the page I need to collect the data that's already in the database. Therefor our
edit function retrieves this data, then passes on to
add to collect the normal data.
The
save function validates the $_POST data. If it fails it check if the primairy Id was present and decides based on that whether it redirects to add or edit.
If the
save function passes I sanitize the userinput before passing it off to a model that saves it to a database using the method save (which checks inside the model if the $id parameter passed was larger than 0. If so it performs an UPDATE else it performs an INSERT.
In the view I just check if an $edit array is present (this is namespaced in our company). If so it matches against the available options in the dropdown and selects the one that matches.
I'm curious how others do this that's why I posted 'my way'.
Code:
<?php
/*
* Controller
*/
class Somecontroller extends Controller {
function Somecontroller()
{
parent::Controller();
}
public function add()
{
/* example some dropdown from database */
$this->load->model('category_model','category');
$this->formdata['categories'] = $this->category->findAll();
$this->_renderform();
}
public function edit($id=NULL)
{
/* function to check for real integer
* see: http://nl3.php.net/manual/en/function.is-int.php#82857
*/
if(isInteger($id))
{
/* load the model with the data to edit */
$this->load->model();
$this->formdata['edit'] = $this->...->read($id);
}
$this->add();
}
public function save()
{
$this->load->library('form_validation');
/* your form validation rules here */
$this->form_validation->...
if ($this->form_validation->run() == FALSE){
/* we need to check id since it might have been triggered by form_validation */
$id = $this->input->post('id');
if ($id > 0 && isInteger($id)) {
$this->edit($this->input->post('id'));
} else {
$this->add();
}
}
else
{
/* sanitize data */
$id = ($_POST['id'] != 0) ? filter_var($_POST['ID_Task'], FILTER_SANITIZE_NUMBER_INT) : NULL;
$data['ID_User'] = ($_POST['ID_User'] != 0) ? filter_var($_POST['ID_User'], FILTER_SANITIZE_NUMBER_INT) : NULL;
/* load whatever model you're going to save your data to */
$this->load->model();
$this->...->save($data, $id);
/* after a POST it's better to user redirect instead of $this->index because
* people tend to press F5 or refresh which gives us unsuspected behaviour
*/
redirect('/somecontroller');
}
private function _renderform()
{
$this->load->library('form_validation');
$data = $this->_getPageStuff();
$data['form'] = $this->load->view('forms/exampleform_view',$this->formdata,TRUE);
$this->load->view('templateview', $data);
}
private function _getPageStuff()
{
/* load page wide settings like custom title, metatags, description etc etc. */
$data[''] = $this->...
return $data;
}
}
?>
And the view
Code:
/**
* forms/exampleform_view
*/
<h3>An example form</h3>
<form action="/somecontroller/save" method="post">
<fieldset>
<?php echo validation_errors(); ?>
<p>
<label for="categoryId">Category</label>
<select name="categoryId">
<?php foreach ($categories as $category) {
echo "<option value=\"".$category['categoryId']."\" ".set_select('categoryId', $category['categoryId'], (isset($edit['categoryId'])&& $category['id']==$edit['categoryId']) ? TRUE : FALSE).">".$category['name']."</option>\n";
} ?>
</select>
</p>
<input type="hidden" name="id" value="<?php echo (isset($edit['id'])) ? $edit['id'] : 0; ?>" />
<input type="submit" value="Submit" />
</fieldset>
</form>