2010-10-04 45 views
0

我創建一個多步驟的形式,我在努力圍繞的ID傳遞給表中的每一步,下面是我的代碼PHP幫助多步形式

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放入表單的最後一部分。

非常感謝

+0

使用會話 – 2010-10-04 13:34:10

+0

我建議你提供的代碼只提供給你問的問題直接相關的部分。如果問題更清楚,您可能會得到更好或更快的答案。謝謝。 – kevtrout 2010-10-04 19:16:06

回答

1

首先,我想你使用$這 - > DB-> INSERT_ID檢索數據庫中的最後插入的ID。您使用2個不同的電話來獲取最後一個插入ID。 第一張:

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

第二個:

$這 - > career_id = $這個 - > DB-> INSERT_ID();

我建議你在第二次調用時使用session來檢索carrer id。

此外,保存方法返回(對於所有模型)是什麼?

1

我認爲變量$this->db在此函數/類中不可用。

如果您將的輸出記錄在類careers,功能save中,那麼您將獲得正確的ID。我很確定這個功能可以在你的工作Model