如何解决在功能中适当添加功能
| 我正在创建一个配置文件脚本,用户可以在其中编辑其个人信息,兴趣和链接。 我把所有字段都以一种形式存在,但是现在我想用制表符将它们分开。因此,我将有一个个人信息标签,兴趣标签和链接标签。在每个页面中,我将有一个表单将数据提交到相应的功能。例如,如果您正在编辑个人信息,则表格将直接指向“ 0” 函数应如下所示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,则这些函数应在一个类中:
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段将作为参数传递给函数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。