1

i have a simple codeigniter form , where there is a checkbox like below:

 <th>  <input class="acb" type="checkbox"  name="generateawb[]" value="<?=$val->id?>" onclick="deRequire('acb')" required /></th>
  <button type="submit" name="savemedaa" style="margin-left:5px; font-size: 0.6em; " class="btn btn-warning">GENERATE AWB</button>

so when the user selects either one or multiple checkboxes, it should insert the selected datas to another table, my controller is like below:

public function generateawb() {
  $data = array();
    if(isset($_POST['savemedaa']))
    {

      $checkboxs = $_POST['generateawb'];
      $generateawb = $this->excel_import_model->selectawb($checkboxs);

foreach($generateawb as $row){
  $clientid= $row->id;
  $batchnumber= $row->batchnumber;

}
$awbg = random_int(100000000, 999999999);
$awb='LRG'.$awbg;

        $data[] = array(
          'clientid'=>$clientid,
          'awb'=>$awb,
           'batchnumber'=>$batchnumber,

        );

      $this->excel_import_model->generateawb($data);

    }
      }
}

and my model is like below:

    public function selectawb($checkboxs) {
                            $this->db->select('*');
                            $this->db->where_in("id", $checkboxs);
                            $this->db->from('fileupload');
                         $query = $this->db->get();
                         $result = $query->result();
                         return $result;

                        }

                        function generateawb($data){
                            $this->db->insert_batch('consignments', $data);
                        }

now the issue is, even if user selects multiple checkboxes only one checkbox is saved to database, can anyone please tell me what is wrong in here, thanks

1 Answer 1

1

You are putting $data[] outside the foreach so it has only one value. Just put it inside:

public function generateawb()
    {
        $data = array();
        if (isset($_POST['savemedaa'])) {

            $checkboxs = $_POST['generateawb'];
            $generateawb = $this->excel_import_model->selectawb($checkboxs);

            foreach ($generateawb as $row) {
                $clientid = $row->id;
                $batchnumber = $row->batchnumber;
                $awbg = random_int(100000000, 999999999);
                $awb = 'LRG' . $awbg;

                $data[] = array(
                    'clientid' => $clientid,
                    'awb' => $awb,
                    'batchnumber' => $batchnumber,

                );
            }

            $this->excel_import_model->generateawb($data);
        }
    }

You should use a formatter to avoid errors like this. If you use VSCode you can use this extention

Sign up to request clarification or add additional context in comments.

Comments

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.