• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
save and edit share same method?

#1
Hi guys,

I have, save() and edit() methods and forms.
They both share same save method save_qa()  for saving data.

With save() method and form when it call front/save_qa It has no error.
But with edit() method and form and when it call front/save_qa it show me this error below

Code:
404 Page Not Found


Here are the codes for save_qa() method.
Code:
  public function save_qa()
   {
       $new_or_edit = $this->session->userdata('new_or_edit');
       $record_id   = $this->session->userdata('record_id');
       
       $data_qa = array();
       if( $this->require_min_level(1) )
       {        
           $data_qa['qa_user']     = $this->auth_user_name;
       }        
       $data_qa['question']    = $this->input->post('question', TRUE);
       $data_qa['answer']      = $this->input->post('answer', TRUE);
       $data_qa['info']        = $this->input->post('info', TRUE);
       $data_qa['language']    = $this->input->post('languages', TRUE);
       $data_qa['version']     = $this->input->post('version', TRUE);
       $data_qa['topic']       = $this->input->post('topics', TRUE);
       $data_qa['time']        = $this->input->post('time', TRUE);

       $this->load->library('form_validation');
       
       $this->form_validation->set_rules('question', 'Question', 'required');
       $this->form_validation->set_rules('answer', 'Answer', 'required');
       $this->form_validation->set_rules('info', 'Info');
       $this->form_validation->set_rules('languages', 'Language', 'required');
       $this->form_validation->set_rules('version', 'Version', 'required');
       $this->form_validation->set_rules('topics', 'Topic', 'required');
       $this->form_validation->set_rules('time', 'Time', 'required');

       if ($this->form_validation->run() == false)
       {
           //If validation is false display the form again.
           if($new_or_edit === 'new') {
               $this->postqa();
           }
           if($new_or_edit === 'edit') {
               $this->edit($record_id);
           }            
       }
       else
       {
           //If validation is TRUE save the data.
           //Display successful message
           //display Q&A form again.
           if($new_or_edit === 'new') {
               $this->qa_mdl->save_qa($data_qa);
           }    
           if($new_or_edit === 'edit') {
               $this->qa_mdl->update_qa($data_qa);
             
           }              
           
           //$this->postqa();
           echo '<br><br><br><br><br>';
           echo '<center><h2>Successfully save.</h2>';
           echo 'Will auto redirect back in 3 seconds...';
           if($new_or_edit === 'new') {
               $url = base_url(). 'front/postqa/';
           }  
           if($new_or_edit === 'edit') {
               $url = base_url(). 'front/yourqa/';
           }  
           header("Refresh: 3;url=$url");
           
       }            
   }

So what could be the culprit?

thanks in advance.
Reply

#2
After updating a record, you are redirecting to front/yourqa.
Is "yourqa" an existing method in your Front.php controller?
Reply

#3
I use another approach.

Url call for adding new record:
front/add_record

Url call for editing existing record;
front/edit_record/415  
(in this example, I would edit record # 415)

Controller Front.php:
PHP Code:
public function add_record()
{
 
  $this->edit_record(0);
}

public function 
edit_record($id 0)
{
 
  //set form validation rules
 
  if ($this->form_validation->run() == FALSE) {
 
     if ($id 0) {
 
        $record $this->qa_mdl->get_record($id);
 
     }
 
     else {
 
        $record = array(
 
          'id' => 0,
 
          'name' => NULL,
 
          'address' => NULL
         
);
 
     }
 
     $data['record'] = $record;
 
     $this->load->helper('form');
 
     $this->load->view('form_record',$data);
 
  }
 
  else {
 
     $data = array(
 
        'name' => $this->input->post('name'),
 
        'address' => $this->input->post('address')
 
     );
 
     $this->qa_mdl->save_record($data,$id);
 
  }


Model Qa_mdl.php:
PHP Code:
public function get_record($id
{
 
  $query $this->db->get_where('qa_table','id=' .$id);
 
  if ($query->num_rows() > 0) {
 
      return $query->row_array();
 
  }
 
  else {
 
      return NULL;
 
  }
}

public function 
save_record($data,$id)
{
 
   if ($id 0) {
 
     $this->db->update('qa_table',$data,'id='.$id);
 
   }
 
   else {
 
      $this->db->insert('qa_table',$data);
 
   }


View form_record.php :
PHP Code:
<?php
// form validation errors here
extract($record);
echo 
form_open('front/edit_record/' $id);
echo 
'<p>form_input('name',$name) . '</p>';
echo '
<p>form_input('address'$address) . '</p>';
echo 
form_submit('submit''Submit form');
echo 
form_close();
?>
Reply

#4
I would propose a different pattern, whether we add or edit is not information that needs to be kept within session. Here the link can tell what is going to happen:

something/add
something/edit/15

Code:
class Something extends CI_Controller {

    protected $edit_mode = true;

    public function __construct() {

        parent::__construct();
    }

    public function add() {

        $this->edit_mode = false;
        return $this->edit();
    }

    public function edit() {

        $id = $this->edit_mode
            ? (int) $this->uri->rsegment(3) // Read the target $id, adjust the rooted segment number, if it is necessary.
            : null;                         // For adding we would not need $id, it is assigned by the server.

        if ($this->edit_mode) {

            // Validate the target $id, check whether the corresponding row exists.
            if (empty($id)) { // This is an oversimplified check.

                // Set an error message (flash).
                // redirect to the index page.
            }
        }

        // Form Validation
            // Insert or update, depending on $this->edit_mode

        $data = array(
            ...
            ...
            'edit_mode' => $this->edit_mode,
        );

        $this->load->view('edit_something', $data);
    }
}
Reply

#5
"a method should do one thing and do it well". its very tempting to create very long methods like you are doing that do many different tasks. the problem is that this is basically exactly the same as procedural programming. why is this an issue? because you end up with "spaghetti code" that is very difficult to maintain.

you are thinking - oh hey i'm making one method to take care of different conditions this is very smart and efficient right? no - its not efficient because in your code you are constantly having to ask questions like - is this new? is this an edit? is this valid? what is my name again?

the longer the method - the more time it takes to debug. because you are typically having to sift through a bunch of conditions. short methods that do one thing - its very fast to find the issue or to update them.

the key is to build like you are doing - but then refactor into smaller methods. at first this might seem like its 'more code' but it ends up being much cleaner and easier and faster to work with. for example put the validation in its own method - it then returns either true or false. if false you show the form again. the validation method does not need to know if this a new insert or an edit.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.