开发者

Properly adding functions within function

I'm creating a profile script where users can edit their personal info, interests, and links.

I had all the fields in one form but now I want to separate them by tabs. So I will have a personal info tab, interests tab, and links tab. In each page I will have a form submitting data to the corresponding function. For example if you're editing the personal info the form will direct to mysite.com/edit/personal_info

The functions should look like this

function edit() {

   function personal_info() {
      //data
   }   

   function interests() {
     //data
   }

   function links() {
     //data
   }

}

I'm not sure how to properly send data from the edit() function to all its sub functions.

I'm adding the general data below to all my function but I want to add it once and all the functions should have it. I'm also trying to avoid global variables.

$this->db->where('user_id', $this->tank_auth->get_user_id());
$query = $this->db->get('user_profiles');
$data['row'] = $query->row();
开发者_开发问答

Then in each sub function I have validation rules (codeigniter) Below are the rules for the personal_info function

$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|xss_clean|min_length[2]|max_length[20]|alpha');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|xss_clean|min_length[2]|max_length[20]|alpha');
$this->form_validation->set_rules('gender', 'Gender', 'trim|required|xss_clean|alpha');

and a statement to add the data to the database or return an error if validation rules fail

if ($this->form_validation->run() == FALSE) //if validation rules fail
        {           
            $this->load->view('edit_profile', $data);
        }
        else //success
        {
        $data = array (                 
                'first_name'    => $this->input->post('first_name'),
                'last_name'     => $this->input->post('last_name'),
                'gender'    => $this->input->post('gender')

            );
            $this->load->model('Profile_model');
            $this->Profile_model->profile_update($data);            
        }

How can I properly create these sub function without repeating code in each one?


Wow, you kind of lost me. Why are you using functions within functions? If you're using CodeIgniter, those functions should be within a class:

class Edit extends CI_Controller {
  function personal_info() {
    /* Do personal info stuff. */
  }

  function interests() {
    /* Do interests stuff. */
  }

  function links() {
    /* Do links stuff. */
  }

  function _common() {
    // The underscore makes the function not available to browse, but you can
    // put common code here that is called within the other functions by
    // invoking $this->_common();
  }
}


by the way your code is made, it looks like you're using codeigniter.

When you request mysite.com/edit/personal_info, it will request a controller called edit, and a function called personal_info, so you don't need a function inside a function, you just need functions inside of the edit controller class. Further url segments will be passed to the function as arguments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜