2014-01-18 54 views
-1

控制器如何會話數據發佈到模型插入到數據庫中的笨

public function respondent_info() 
    {  
     $data['states'] = $this->survey_model->get_states(); 
     $state = $this->session->store['loginData']['state']; 
     $financial_year = $this->session->store['loginData']['financial_year']; 
     $getData = $this->survey_model->get_page($state, $financial_year); 
     if ($getData == '1') { 
      $data['res_info'] = $this->survey_model->get_resinfo_details($state); 
      $this->load->view('edit_respondent_info', $data);  
     }else { 
      $this->load->view('respondent_info', $data); 
     } 
     if(isset($_POST['submit_x'])) { 
      $respondentArray = array(
       'state' => empty($_POST['state']) ? '' : $_POST['state'], 
       'name' => empty($_POST['name']) ? '' : $_POST['name'], 
       'title1' => empty($_POST['title1']) ? '' : $_POST['title1'], 
       'dline' => empty($_POST['dline']) ? '' : $_POST['dline'], 
       'email' => empty($_POST['email']) ? '' : $_POST['email'], 
       'organization' =>empty($_POST['organization']) ? '' : $_POST['organization'], 
       'address' => empty($_POST['address']) ? '' : $_POST['address'], 
       'city' => empty($_POST['city']) ? '' : $_POST['city'], 
       'state1' =>empty($_POST['state1']) ? '' : $_POST['state1'], 
       'zip' => empty($_POST['zip']) ? '' : $_POST['zip'], 
       'phone' => empty($_POST['phone']) ? '' : $_POST['phone'], 
       'fax' =>empty($_POST['fax']) ? '' : $_POST['fax'], 
      ); 
      $this->session->set_userdata('respondent_info',$respondentArray); 
      $this->survey_model->auto_save_respondent_info($_POST); 
      redirect('survey/budget_overview'); 
     } 
    } 

模式

function auto_save_respondent_info($post){ 
    echo("auto"); 
    $respondentArray = array(
      'survey_id' => $this->session->store['survey_id'], 
      'state' => empty($_POST['state']) ? '' : $_POST['state'], 
      'name' => empty($_POST['name']) ? '' : $_POST['name'], 
      'title1' => empty($_POST['title1']) ? '' : $_POST['title1'], 
      'dline' => empty($_POST['dline']) ? '' : $_POST['dline'], 
      'email' => empty($_POST['email']) ? '' : $_POST['email'], 
      'organization' =>empty($_POST['organization']) ? '' : $_POST['organization'], 
      'address' => empty($_POST['address']) ? '' : $_POST['address'], 
      'city' => empty($_POST['city']) ? '' : $_POST['city'], 
      'state1' =>empty($_POST['state1']) ? '' : $_POST['state1'], 
      'zip' => empty($_POST['zip']) ? '' : $_POST['zip'], 
      'phone' => empty($_POST['phone']) ? '' : $_POST['phone'], 
      'fax' =>empty($_POST['fax']) ? '' : $_POST['fax'], 
     ); 

    $this->db->select('*'); 
    $this->db->from('survey_respondent_info'); 
    $this->db->where('state', $state); 
    $query = $this->db->get(); 
     if ($query->num_rows() > 0) 
     { 
     echo("update"); 
      // $this->db->where('state', $state); 
      // $this->db->update('survey_respondent_info', $resInfoArray); 
     } 
     else{ 
     echo("insert"); 
      // $this->db->insert('survey_respondent_info', $resInfoArray); 
     } 
    } 

這裏沒有調用模型函數,我不能查看echo中給出的數據。如何調用它或如何將會話中存儲的數據發送到模型,我知道我的代碼中缺少某些東西,請有人可以幫助我。謝謝。

回答

0

[根據用戶需求更新] 首先,您需要驗證用戶輸入。你可以找到示例here。接下來你必須在使用它之前加載模型。您可以從控制器加載模型像

$this->load->model('survey_model'); 

,如果驗證是真的,你可以撥打下面的保存功能:

$this->survey_model->auto_save_respondent_info(); 

你不必會話或交的數據傳遞給你的模型。

,你可以簡單地從模型訪問會話像

function auto_save_respondent_info(){ 
     $respondent_info = $this->session->userdata('respondent_info'); 
     print_r($respondent_info); 
     ............... 
    } 

您可以從模型

function auto_save_respondent_info(){ 
     print_r($_POST); 
     ............... 
    } 

訪問後的數據由於您使用codeignitor你可以使用$this->input->post('fieldname')代替$ _ POST。此外,您不必將發佈數據保存到會話以傳遞模型。你可以在你的模型中訪問$this->input->post('fieldname')。你也應該在控制器中驗證你的表單輸入。在示範

function auto_save_respondent_info(){ 
     echo $this->input->post('state'); 
     .......... 

} 

訪問後的數據,我希望你明白了吧。

+0

對不起,它不是打印任何 – user3165411

+0

我需要的值存儲在會話中,也發送這些模型,以便存儲在分貝 – user3165411

+0

您可以訪問您從模型發佈數據$ this-> input-> post ( '字段名');可以將數據保存到會話相同你沒有 –

相關問題