2011-05-31 57 views
0

我正在创建一个配置文件脚本,用户可以在其中编辑其个人信息,兴趣和链接。在函数内正确添加函数

我有一个表单中的所有字段,但现在我想通过制表符将它们分开。所以我会有一个个人信息标签,兴趣标签和链接标签。在每个页面中,我将有一个表单将数据提交给相应的函数。例如,如果您正在编辑的个人信息表格将直接向mysite.com/edit/personal_info

的功能应该是这样的

function edit() { 

    function personal_info() { 
     //data 
    } 

    function interests() { 
    //data 
    } 

    function links() { 
    //data 
    } 

} 

我不知道如何正确地从编辑()函数将数据发送到所有的子功能。

我将下面的一般数据添加到我的所有功能,但我想添加一次,所有的功能应该有它。我也试图避免全局变量。

$this->db->where('user_id', $this->tank_auth->get_user_id()); 
$query = $this->db->get('user_profiles'); 
$data['row'] = $query->row(); 

然后在每个子功能我有验证规则(笨)下面是对的personal_info功能

$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'); 

和语句将数据添加到数据库或返回如果验证错误的规则规则失败

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);    
     } 

如何正确地创建这些子函数而不必在每个代码中重复代码?

回答

3

哇,你有点迷失了我。你为什么在函数中使用函数?如果你使用的是CodeIgniter,那么这些函数应该属于一个类:

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(); 
    } 
} 
+1

这当然会出现在名为controllers/edit.php的文件中。欲了解更多信息,我鼓励您查看本站的控制器文档:http://codeigniter.com/user_guide/general/controllers.html – 2011-05-31 18:03:32

+0

谢谢你的例子!我忘记了CI的课程。我使用了'$ this - > _ common();'按照说明操作! :) – CyberJunkie 2011-05-31 18:14:57

+0

很高兴它的作品,并与您的网站祝你好运!我使用CodeIgniter相当多,我想你会发现它是一个方便的框架来使用。 – 2011-05-31 18:22:18

2

通过您的代码制作方式,它看起来像您使用codeigniter。

当您请求mysite.com/edit/personal_info时,它会请求一个名为edit的控制器和一个名为personal_info的函数,因此您不需要函数内部的函数,只需要编辑控制器内部的函数类。更多的url段将作为参数传递给函数。

+0

谢谢你的解释! – CyberJunkie 2011-05-31 18:15:58