Here's my idea: let's say my form has 20 fields.
I have two methods: add, update.
Defining each field inside mentioned methods will take too long time.
I'm looking for the way to define my fields once and send them to above methods.
How to do it? Is there simpler way?
Should I create a method for defining my fields and additionally a method to list my fields in view?
Please, have a look at my code:
My controller:
PHP Code:
function add() {
if ( $this->form_validation->run() ) { //validation success: add data and redirect
$data = array(
'title' => $this->input->post( 'title' ),
'content' => $this->input->post( 'content' ),
);
$jammy_id = $this->Jammy_model->add_jammy( $data );
redirect( 'jammy2/index' );
} //validation not successfull
else { //params for the view and input data from user
$params[ '_view' ] = 'jammy/edit';
$params[ 'action' ] = 'jammy2/add';
$params[ 'title' ] = $this->input->post( 'title' );
$params[ 'content' ] = $this->input->post( 'content' );
$this->load->view( 'layouts/main', $params );
}
}
//________________________________________________________________________________________
function update( $id = NULL ) {
if ( $this->Jammy_model->get_jammy( $id ) ) { //check if jammy exists in db table
$data[ 'jammy' ] = $this->Jammy_model->get_jammy( $id );
} else { //jammy ain't exist, show 404
show_404();
}
if ( $this->form_validation->run() ) { ////validation success: set and add data, redirect
$data = array(
'title' => $this->input->post( 'title' ),
'content' => $this->input->post( 'content' ),
);
$jammy_id = $this->Jammy_model->update_jammy( $id, $data );
redirect( 'jammy2/index' );
} else { //params for the view and input data from user
$params[ '_view' ] = 'jammy/edit';
$params[ 'action' ] = 'jammy2/update/' . $id;
$params[ 'title' ] = $data[ 'jammy' ][ 'title' ]; //dane z bazy
$params[ 'content' ] = $data[ 'jammy' ][ 'content' ]; //dane z bazy
$this->load->view( 'layouts/main', $params );
}
}
My view contains only field variables like $title, $content etc.
My model contains only simple methods for db operations.