duancai7002 2014-01-18 06:45
浏览 40

如何将会话数据发布到模型中以插入到codeigniter中的db中

controller

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');
        }
    }  

model

 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);
        }
  }

here it is not calling the model function, am not able to view the data given in echo. How to call it or how to send the data stored in session to model, i know that something missing in my code, can someone help me please. thanks.

  • 写回答

1条回答 默认 最新

  • dongxing2302 2014-01-18 06:49
    关注

    [Updated as per user need] First of all you need to validate the user inputs. You can find example here. Next you have to load the model before using it. You can load a model from controller like

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

    And if validation is true you can call the save function as below:

    $this->survey_model->auto_save_respondent_info();
    

    You dont have to pass session or post data to your model.

    you can simply access the session from model like

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

    You can access the post data from model as

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

    Since you are using codeignitor you could use $this->input->post('fieldname') instead of $_POST. Also you dont have to save the post data to session to pass it model. You can access $this->input->post('fieldname') in you model. Also you should validate your form inputs in controller.

    Access post data in Model

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

    I hope you get the point.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)