【问题标题】:How to send value if input not empty in insert batch CodeIgniter如果在插入批处理 CodeIgniter 中输入不为空,如何发送值
【发布时间】:2023-03-28 08:17:01
【问题描述】:

基本上我有这个将输入的值发送到数据库:

    $tag = $this->input->post('tags[]');

    $data = array();

    foreach ($tag as $term){
        $data[] = array(
            'post_id'  => $post_id,
            'term_id'  => $term,
            'user_id'  => $this->input->post('user_id'),
            'status'  => 'attached',
            'type'     =>  'tag',
        );

    }

    $this->db->insert_batch('ci_relationship', $data);

但我试图通过让管理员输入一个 X-user 作为值或他的会话 user_id 来使 user_id “可选”,以防他决定将其留空..

我试过这样做,但还是不行:

    $tag = $this->input->post('tags[]');

    $data = array();

    foreach ($tag as $term){
        $data[] = array(
            'post_id'  => $post_id,
            'term_id'  => $term,
            //'user_id'  => $this->input->post('user_id'),
            'status'  => 'attached',
            'type'     =>  'tag',
        );

    }

    $user_id = $this->input->post('user_id');

    if(empty($user_id)){
        $data['user_id'] = $this->session->userdata('user_id');
    } else {
        $data['user_id'] = $user_id;
    }

    $this->db->insert_batch('ci_relationship', $data);

如您所见,我在 foreach 中注释了 user_id,然后我将其添加到下一个代码块中,该代码块应该检查输入是否为空。

提前致谢!

【问题讨论】:

    标签: php codeigniter codeigniter-3 bulkinsert


    【解决方案1】:

    那个 if 语句必须在那个 foreach 里面。

    $tag = $this->input->post('tags[]');
    $user_id = $this->input->post('user_id');
    
    $data = array();
    
    foreach ($tag as $key => $term){
        $data[$key] = array(
            'post_id'  => $post_id,
            'term_id'  => $term,
            'status'  => 'attached',
            'type'     =>  'tag',
        );
    
        if(empty($user_id)){
            $data[$key]['user_id'] = $this->session->userdata('user_id');
        } else {
            $data[$key]['user_id'] = $user_id;
        }
    }
    
    $this->db->insert_batch('ci_relationship', $data);
    

    【讨论】:

      【解决方案2】:

      我认为不需要添加更多的行,只需在旁边执行此操作即可

      $data[] = array(
              'post_id'  => $post_id,
              'term_id'  => $term,
              'user_id'  => (!empty($this->input->post('user_id')) ? $this->input->post('user_id') : '',
              'status'  => 'attached',
              'type'     =>  'tag',
          );
      

      【讨论】:

        【解决方案3】:
        $tag = $this->input->post('tags[]');
        
        $data = array();
        
        foreach ($tag as $key => $term){
        if(empty($this->input->post('user_id')))
        {
        $data[$key] = array(
                'post_id'  => $post_id,
                'term_id'  => $term,
                'status'  => 'attached',
                'type'     =>  'tag',
            );
        
        }
        else
        {
        $data[$key] = array(
                'post_id'  => $post_id,
                'term_id'  => $term,
                'user_id'  => $this->input->post('user_id'),
                'status'  => 'attached',
                'type'     =>  'tag',
            );
        
        }
        }
        
        $this->db->insert_batch('ci_relationship', $data);
        

        希望它对你有用。

        【讨论】:

          猜你喜欢
          • 2017-04-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-19
          • 1970-01-01
          • 1970-01-01
          • 2019-06-12
          • 1970-01-01
          相关资源
          最近更新 更多