【问题标题】:insert variable value to database from another function in codeigniter?从codeigniter中的另一个函数将变量值插入数据库?
【发布时间】:2015-05-27 09:00:57
【问题描述】:

我在这里需要一些帮助。

这是我的控制器:

function result_selected_abc_data(){
$this->auth->restrict();
$this->load->model('usermodel');
$this->load->model('productmodel');
$level=$this->session->userdata('level');
$this->load->library('form_validation');

$this->form_validation->set_rules('date1','Tanggal  Di Masukkan','trim|required');
$this->form_validation->set_rules('date2','Tanggal Belum Di Masukkan','trim|required');
$this->form_validation->set_error_delimiters('<span style="color:#FF0000;">','</span>');

if($this->form_validation->run() == false){
$data['parent']=$this->usermodel->get_data_parent_menu($level);
$data['first_child'] = $this->usermodel->get_data_first_child_menu($level);
$data['second_child'] = $this->usermodel->get_data_second_child_menu($level);
$data['third_child'] = $this->usermodel->get_data_third_child_menu($level);
$data['fourth_child'] = $this->usermodel->get_data_fourth_child_menu($level);
$data['fifth_child'] = $this->usermodel->get_data_fifth_child_menu($level);
$data['sixth_child'] = $this->usermodel->get_data_sixth_child_menu($level);
$data['seventh_child'] = $this->usermodel->get_data_seventh_child_menu($level);
$this->template->display('ABC_select_date',$data);
}else{

$data['selected_date']= array(
          'date1' => $this->input->post('date1'),
          'date2' => $this->input->post('date2')
          );
$date1=$this->input->post('date1');
$date2=$this->input->post('date2');
$data['ABC_report_result']= $this->productmodel->get_ABC_data_based_on_date($date1,$date2);
$this->template->display('ABC_select_data_result',$data);
    }
}

我想在另一个函数中将 $data['ABC_report_result'] 值插入数据库,比如说保存 abc():

function save_abc(){

 **what should I make in here so I can get $data['ABC_report_result'] value and save it to database?**

}

我该怎么做?谢谢您的帮助。

【问题讨论】:

    标签: php codeigniter controller


    【解决方案1】:

    在你的控制器中

    function save_abc(){
    $ArrData = array(
         'database_column'=>'value',
         'database_column2'=>'value2',
    );
    $this->your_model->your_function($ArrData);
    
    }
    

    在你的模型中

    public function your_function($data=''){
    
            $this->db->insert('your_table',$data);
        }
    

    更新解决方案 你可以在你的视图文件中做一件事,在隐藏字段中保存 $date 值并创建一个按钮保存数据,现在你可以调用 action save_abc()

    function save_abc(){
       $this->load->model('productmodel');
       $date1=$this->input->post('date1'); //data from hidden field
       $date2=$this->input->post('date2'); //data from hidden field
       $result = $this->productmodel->get_ABC_data_based_on_date($date1,$date2);
      $this->your_model->your_save_function($result);
    
    }
    

    【讨论】:

    • 我可以理解,但是如何将 $data['ABC_report_result'] 变量中的值从函数 result_selected_abc_data 传递到 save_abc 函数?然后在 save_abc 函数中将该数据保存到数据库中?
    【解决方案2】:

    在 CodeIgniter 中,您的 input 对象是全局可访问的!因此,如果您需要 save_abc 中的数据,为什么不尝试在目标函数中插入 input->post() 方法?

    【讨论】:

    • 因为我需要先通过 result_selected_abc_data 函数向用户显示数据。然后用户将决定是否保存数据。所以这就是为什么我创建另一个函数来将数据保存到数据库中。我仍然不知道该怎么做。
    【解决方案3】:

    试试这个:

    function result_selected_abc_data()
    {
        $this->auth->restrict();
        $this->load->model('usermodel');
        $this->load->model('productmodel');
        $level=$this->session->userdata('level');
        $this->load->library('form_validation');
    
        $this->form_validation->set_rules('date1','Tanggal  Di Masukkan','trim|required');
        $this->form_validation->set_rules('date2','Tanggal Belum Di Masukkan','trim|required');
        $this->form_validation->set_error_delimiters('<span style="color:#FF0000;">','</span>');
    
        if($this->form_validation->run() == false){
            $data['parent']=$this->usermodel->get_data_parent_menu($level);
            $data['first_child'] = $this->usermodel->get_data_first_child_menu($level);
            $data['second_child'] = $this->usermodel->get_data_second_child_menu($level);
            $data['third_child'] = $this->usermodel->get_data_third_child_menu($level);
            $data['fourth_child'] = $this->usermodel->get_data_fourth_child_menu($level);
            $data['fifth_child'] = $this->usermodel->get_data_fifth_child_menu($level);
            $data['sixth_child'] = $this->usermodel->get_data_sixth_child_menu($level);
            $data['seventh_child'] = $this->usermodel->get_data_seventh_child_menu($level);
            $this->template->display('ABC_select_date',$data);
        }
        else
        {
    
            $data['selected_date']= array(
                      'date1' => $this->input->post('date1'),
                      'date2' => $this->input->post('date2')
                      );
            $date1=$this->input->post('date1');
            $date2=$this->input->post('date2');
            $data['ABC_report_result']= $this->productmodel->get_ABC_data_based_on_date($date1,$date2);
    
            $this->productmodel->save_abc_data($data['ABC_report_result']);
    
            $this->template->display('ABC_select_data_result',$data);
        }
    }
    

    我在这里遗漏了什么明显的东西吗?

    【讨论】:

    • 这是不对的。如果我这样做,用户不能决定是否保存数据。这个过程的逻辑是首先向用户显示数据,然后用户将选择是否保存数据。所以这就是为什么我在这里做了两个功能。但我仍然不知道如何将数据从第一个函数(显示数据)传递到第二个函数(保存数据)。
    • 是的,有 2 个选项供您选择 - 但首先我需要知道:既然您在谈论用户 - 您是否使用会话?
    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 2014-11-21
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    相关资源
    最近更新 更多