我正在创建一个配置文件脚本,用户可以在其中编辑他们的个人信息、兴趣和链接。
我把所有字段都放在一个表单中,但现在我想用制表符将它们分隔开。所以我会有一个个人信息选项卡,兴趣选项卡和链接选项卡。在每个页面中,我都会有一张向相应功能提交数据的表格。例如,如果您正在编辑个人信息,表单将直接指向mysite.com/edit/personal_info
功能应该像这个
function edit() {
function personal_info() {
//data
}
function interests() {
//data
}
function links() {
//data
}
}
我不知道如何正确地将数据从edit()函数发送到它的所有子函数。
我将下面的一般数据添加到我的所有函数中,但我想添加一次,所有函数都应该有它。我还试图避免全局变量。
$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);
}
如何正确创建这些子函数而不在每个子函数中重复代码?
哇,你有点迷失了我。你为什么在函数中使用函数?如果您使用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();
}
}
根据代码的生成方式,看起来像是在使用codeigniter。
当你请求mysite.com/edit/personal_info时,它会请求一个名为edit的控制器和一个名为主personal_info的函数,所以你不需要函数中的函数,你只需要edit控制器类中的函数。进一步的url段将作为参数传递给函数。