Welcome Guest, Not a member yet? Register   Sign In
Two functions, one shared view and plenty of fields. Trying to DRY my code
#1

(This post was last modified: 04-13-2019, 11:31 PM by janeiro.)

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? Huh
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.
Reply
#2

I would just create a method for setting them like below, I do not know what
type fields you are using (int string etc; So just a generic method.


PHP Code:
/**
 * call ( $params = $this->setParams($view, $action, $title, $content);
 */
private function setParams($view$action$title$content)
{
 
   $params = [
 
       '_view'   => $view,
 
       'action'  => $action,
 
       'title'   => $title,
 
       'content' => $content,
 
   ];

 
   return $params;

 
Hope that helps.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB