PHP帮助多步骤表单

发布于 2024-09-25 16:23:41 字数 4648 浏览 3 评论 0原文

我正在创建一个多步骤表单,并且我正在努力将 ID 传递到表单的每个步骤,下面是我的代码,

    function add_career() {
    $data = array();
    $this->load->model('admin_model');
    $this->load->library('form_validation');

        if($this->input->post('career_set') == 'Save') {

            $this->form_validation->set_rules('career_name', 'Career name', 'required|min_length[3]');
            $this->form_validation->set_rules('career_desc', 'Career description', 'required|max_length[3000]');
            $this->form_validation->set_rules('useful_info', 'Useful Information', 'max_length[1000]');
            $this->form_validation->set_rules('useful_links', 'Useful Links', 'max_length[1000]');
            if ($this->form_validation->run() == FALSE) {
               $this->template->build('admin/add_career'); 
            } else {
               if($this->input->post('degree_needed')) {
                   $degree_needed = 'Yes';
               } else {
                   $degree_needed = 'No';
               }

               $this->load->model('careers');
               $insertCareer = $this->careers->save(
                 $this->input->post('career_name'),
                 $this->input->post('career_desc'),
                 $degree_needed,
                 $this->input->post('useful_info'),
                 $this->input->post('useful_links')
               );

                   //save the data in the session, so we can to it if need be


                $this->career_id = $this->db->insert_id;
                $this->session->set_userdata($insertCareer);
                //$this->firephp->log($this->session->userdata($this->career_id, 'Career ID'));        
               }
        }

        $this->career_id = $this->db->insert_id();
        if($this->input->post('salary_set') == 'Save') {

                $this->firephp->log($this->career_id);
                $this->form_validation->set_rules('basic_salary', 'Basic salary', 'required|max_length[12]');
                $this->form_validation->set_rules('trained_salary', 'Fully trained salary', 'required|max_length[12]');
                $this->form_validation->set_rules('progressed_salary', 'Progressing onto salary', 'required|max_length[12]');
                $this->form_validation->set_rules('average_salary', 'Average salary', 'required|max_length[12]');

                if ($this->form_validation->run() == FALSE) {
                        $this->template->build('admin/add_career'); 
                } else {
                    $this->load->model('salaries');
                    $insertSalary = $this->salaries->save(
                        $this->input->post('basic_salary'),
                        $this->input->post('trained_salary'),
                        $this->input->post('progressed_salary'),
                        $this->input->post('average_salary'),
                        $this->career_id
                    );

                $this->session->set_userdata($insertSalary);
                $this->firephp->log($insertSalary);    
                }
        }

        if($this->input->post('course_grades_set') == 'Save') {
            //first off we need to save the grade details

            $this->load->model('grades');
            $this->load->model('course');
            $this->firephp->log(count($_POST['grade_desc']));

            foreach ($_POST['grade_desc'] as $k => $v) {
                $this->firephp->log($v, 'Looped Results');
                $insertGrade = $this->grades->save($v, $this->session->userdata('career_id'));
                // theorertically we should be able to save the assicated course at the same time using $k
                $insertCourse = $this->course->save(
                    $_POST['course_type'][$k],
                    $_POST['course_names'][$k], 
                    $_POST['course_links'][$k],
                    $this->db->insert_id()
                );
                $this->firephp->log($insertGrade, $k);
                $this->firephp->log($insertCourse, $k);
            }
            //$insertGrades = $this->grades->save()
            //);
        }




   $this->template->build('admin/add_career', $data);
}

我需要从保存职业的部分获取最后插入的 id,并将其传递给下一步是保存工资详细信息,目前,我得到的只是一个空变量。我的变量是 $this->career_id;

如果有人可以解释我如何将最后插入的 id 放入表单的最后部分。

非常感谢

I am creating a multi-step form, and I am struggling to pass around the ID's to each step of the form, below is my code

    function add_career() {
    $data = array();
    $this->load->model('admin_model');
    $this->load->library('form_validation');

        if($this->input->post('career_set') == 'Save') {

            $this->form_validation->set_rules('career_name', 'Career name', 'required|min_length[3]');
            $this->form_validation->set_rules('career_desc', 'Career description', 'required|max_length[3000]');
            $this->form_validation->set_rules('useful_info', 'Useful Information', 'max_length[1000]');
            $this->form_validation->set_rules('useful_links', 'Useful Links', 'max_length[1000]');
            if ($this->form_validation->run() == FALSE) {
               $this->template->build('admin/add_career'); 
            } else {
               if($this->input->post('degree_needed')) {
                   $degree_needed = 'Yes';
               } else {
                   $degree_needed = 'No';
               }

               $this->load->model('careers');
               $insertCareer = $this->careers->save(
                 $this->input->post('career_name'),
                 $this->input->post('career_desc'),
                 $degree_needed,
                 $this->input->post('useful_info'),
                 $this->input->post('useful_links')
               );

                   //save the data in the session, so we can to it if need be


                $this->career_id = $this->db->insert_id;
                $this->session->set_userdata($insertCareer);
                //$this->firephp->log($this->session->userdata($this->career_id, 'Career ID'));        
               }
        }

        $this->career_id = $this->db->insert_id();
        if($this->input->post('salary_set') == 'Save') {

                $this->firephp->log($this->career_id);
                $this->form_validation->set_rules('basic_salary', 'Basic salary', 'required|max_length[12]');
                $this->form_validation->set_rules('trained_salary', 'Fully trained salary', 'required|max_length[12]');
                $this->form_validation->set_rules('progressed_salary', 'Progressing onto salary', 'required|max_length[12]');
                $this->form_validation->set_rules('average_salary', 'Average salary', 'required|max_length[12]');

                if ($this->form_validation->run() == FALSE) {
                        $this->template->build('admin/add_career'); 
                } else {
                    $this->load->model('salaries');
                    $insertSalary = $this->salaries->save(
                        $this->input->post('basic_salary'),
                        $this->input->post('trained_salary'),
                        $this->input->post('progressed_salary'),
                        $this->input->post('average_salary'),
                        $this->career_id
                    );

                $this->session->set_userdata($insertSalary);
                $this->firephp->log($insertSalary);    
                }
        }

        if($this->input->post('course_grades_set') == 'Save') {
            //first off we need to save the grade details

            $this->load->model('grades');
            $this->load->model('course');
            $this->firephp->log(count($_POST['grade_desc']));

            foreach ($_POST['grade_desc'] as $k => $v) {
                $this->firephp->log($v, 'Looped Results');
                $insertGrade = $this->grades->save($v, $this->session->userdata('career_id'));
                // theorertically we should be able to save the assicated course at the same time using $k
                $insertCourse = $this->course->save(
                    $_POST['course_type'][$k],
                    $_POST['course_names'][$k], 
                    $_POST['course_links'][$k],
                    $this->db->insert_id()
                );
                $this->firephp->log($insertGrade, $k);
                $this->firephp->log($insertCourse, $k);
            }
            //$insertGrades = $this->grades->save()
            //);
        }




   $this->template->build('admin/add_career', $data);
}

I need to get the last inserted id from the section that saves the career, and pass it to the next step that saves the salary details, at the moment, all I get is a null variable. The var i am is $this->career_id;

If anyone can explain how I can get the last inserted id into the last section of the form.

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隐诗 2024-10-02 16:23:41

首先,我假设您使用 $this->db->insert_id 来检索数据库中最后插入的 id。您使用 2 个不同的调用来获取最后一个插入 ID。
第一个:

$this->career_id = $this->db->insert_id;

第二个:

$this->career_id = $this->db->insert_id();

我建议您在第二次通话时使用会话来检索司机 ID。

另外,save 方法返回什么(对于所有模型)?

First of all, I suppose that you use $this->db->insert_id to retrieve the last inserted id in the database. You use 2 different calls to get the last insert id.
The first one :

$this->career_id = $this->db->insert_id;

The second one:

$this->career_id = $this->db->insert_id();

I suggest you use the session to retrieve the carrer id, on the second call.

Also, what does the save method returns (for all models)?

夏了南城 2024-10-02 16:23:41

我认为变量 $this->db 在此函数/类中不可用。

如果您将 $this->db->insert_id(); 的输出记录在类 careers 函数 save 中,那么您将获得正确的 ID。我很确定这个函数可以在您的模型中使用

I think the variable $this->db is not available in this function/class.

If you will log the output of $this->db->insert_id(); in the class careers, function save, then you'll get the right ID. I'm pretty sure this function will work in your Model

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文