0

I want to make my code dry and I'm new to codeigniter I using $this->db->set($data) so I don't need to duplicate many of this set how cound I prevent $this->db->set($data) to skip in_image if it is empty to set it into db when update is it possible ?

My controller:

$data_arr=array(
                        'title'=>$this->input->post('txttitle'),
                        'desc'=>$this->input->post('txtdesc'),
                        'addr'=>$this->input->post('txtaddr'),
                        'fbn'=>$this->input->post('txtfbname'),
                        'fburl'=>$this->input->post('txturl'),
                        'status'=>$this->input->post('status')
            );
            if(!empty($_FILES['File']['tmp_name']))
            {
                $new_file_name=date("mdY")."_".time();
                $config['upload_path']  = './assets/images/';
                $config['allowed_types']= 'gif|jpg|png';
                $config['max_size']     = 2048;
                $config['max_width']    = 640;
                $config['max_height']   = 420;
                $config['file_name']    = $new_file_name;
                $this->load->library('upload', $config);
                if ( ! $this->upload->do_upload('File'))
                {
                    $error = array('error' => $this->upload->display_errors());
                    $image_arr = array('in_image'=>'error');
                }
                else
                {
                    $image_arr = array('in_image'=>$this->upload->data('orig_name'));
                }
            }
            else
            {
                $image_arr = array('in_image'=>'');
            }
            $data_merge = array_merge($data_arr,$image_arr);
            $this->Description_model->update_all($data_merge,$id);

my model

public function update_all($data, $id)
{
    if($data['in_image'] != 'error' && empty($data['in_image'])
    {
        /*
          this is where i want to check if in_image is empty than prevent 
          $this->db->set($data) skip to set in_image
        */
        $this->db->set($data)
             ->where('in_id',$id);
        if($this->db->update('tbl_desc'))
        {return TRUE;}
        else
        {return FALSE;}
    }
    elseif($data['in_image'] != 'error' && $data['in_image'] != ''))
    {

    }
    else
    {return FALSE;}
}

Thank in advance

2 Answers 2

1

Your post is confusing, but it seems you want to prevent the UPDATE whenever in_image is empty, correct?

$data = array
(
   'title'   => $this->input->post('txttitle'),
   'desc'    => $this->input->post('txtdesc'),
   'addr'    => $this->input->post('txtaddr'),
   'fbn'     => $this->input->post('txtfbname'),
   'fburl'   => $this->input->post('txturl'),
   'status'  => $this->input->post('status')
);

if(!empty($_FILES['File']['tmp_name']))
{
   $new_file_name           = date("mdY")."_".time();
   $config['upload_path']   = './assets/images/';
   $config['allowed_types'] = 'gif|jpg|png';
   $config['max_size']      = 2048;
   $config['max_width']     = 640;
   $config['max_height']    = 420;
   $config['file_name']     = $new_file_name;

   $this->load->library('upload', $config);

   if($this->upload->do_upload('File'))
      $data['in_image'] = $this->upload->data('orig_name');
}
/*
 * Checks if the `in_image` index exists in the array.
 * Only if the index exists is when it allows the update to happen.
 */
if(isset($data['in_image']) == TRUE)
   $this->Description_model->update_all($data, $id);

Now in your model all you need is:

public function update_all($data, $id)
{
   $this->db->where('id', $id)
            ->update('table_name', $data);
}
Sign up to request clarification or add additional context in comments.

Comments

0

In codeigniter, data can be updated mainly 2 way,

$data  = array{
          'YOUR_COL_1' => 'YOUR_VALUR_1',
          'YOUR_COL_2' => 'YOUR_VALUR_2',
};
$this->db->where('id', $id);
$this->db->update('YOUR_TABLE', $data);

OR

$this->db->set('YOUR_COL_1', 'YOUR_VALUR_1');
$this->db->set('YOUR_COL_2', 'YOUR_VALUR_2');//if 2 columns
$this->db->where('id', $id);
$this->db->update('YOUR_TABLE');

Also there is another closing bracket needed in if condition as well as !empty

if($data['in_image'] != 'error' && !empty($data['in_image']))

1 Comment

does no solution other than duplicate $this->db->set('',''); for every array data i had, your 1st solution my code already done it just i want to check if in_image is empty then prevent $this->db->set($data); to set in_image=> '' to update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.