Php 在函数中正确添加函数
我正在创建一个配置文件脚本,用户可以在其中编辑他们的个人信息、兴趣和链接 我把所有字段都放在一个表单中,但现在我想用制表符将它们分开。所以我会有一个个人信息标签,兴趣标签和链接标签。在每个页面中,我都会有一个表单,将数据提交给相应的函数。例如,如果您正在编辑个人信息,表单将指向Php 在函数中正确添加函数,php,function,codeigniter,Php,Function,Codeigniter,我正在创建一个配置文件脚本,用户可以在其中编辑他们的个人信息、兴趣和链接 我把所有字段都放在一个表单中,但现在我想用制表符将它们分开。所以我会有一个个人信息标签,兴趣标签和链接标签。在每个页面中,我都会有一个表单,将数据提交给相应的函数。例如,如果您正在编辑个人信息,表单将指向mysite.com/edit/personal\u info 函数应该是这样的 function edit() { function personal_info() { //data }
mysite.com/edit/personal\u 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();
然后在每个子函数中,我都有验证规则(codeigniter),下面是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
当您请求mysite.com/edit/personal_info时,它将请求一个名为edit的控制器和一个名为personal_info的函数,因此您不需要函数中的函数,只需要edit controller类中的函数。进一步的url段将作为参数传递给函数。通过编写代码的方式,看起来您正在使用codeigniter
当您请求mysite.com/edit/personal_info时,它将请求一个名为edit的控制器和一个名为personal_info的函数,因此您不需要函数中的函数,只需要edit controller类中的函数。更多的url段将作为参数传递给函数。哇,你有点迷路了。为什么要在函数中使用函数?如果您使用的是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,那么这些函数应该在一个类中:
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();
}
}
当然,这将位于名为controllers/edit.php的文件中。有关更多信息,我鼓励您查看此站点控制器的文档:谢谢您提供的示例!我已经忘记了CI课程。我使用了
$this->\u common()代码>按说明执行,它可以正常工作!:)很高兴它能工作,祝你的网站好运!我经常使用CodeIgniter,我想您会发现它是一个方便使用的框架。当然,它应该位于名为controllers/edit.php的文件中。有关更多信息,我鼓励您查看此站点控制器的文档:谢谢您提供的示例!我已经忘记了CI课程。我使用了$this->\u common()代码>按说明执行,它可以正常工作!:)很高兴它能工作,祝你的网站好运!我经常使用CodeIgniter,我想您会发现它是一个方便的框架。