0

I have an array that I will insert into the item table, here I use multiple inserts

Array
(
    [0] => Array
        (
            [id_service] => 2
            [tracking_number] => RJC219384044389234035
        )

    [1] => Array
        (
            [id_service] => 1
            [tracking_number] => RJC749944771469498146
        )
)

in the item table, there is already id_service: 2 how to make validation, when one of the input is already in the item table, then all the input is false so it fails?

my code

public function nyobain()
  {
    $resi             = $this->input->post('kode'); //tracking_number
    $expld            = explode(' ', $resi);
    $data             = $this->M_outbound_destinasi->dbGet($expld); //get 'id_service' where tracking_number

    // $db  = $this->db->query("SELECT id_service FROM `tabel_items`")->result();
    // foreach ($db $key) {
    //     $temp=array($key->id_service);
    // }
    // how to cek ? if id_service already in the item table

    foreach ($data as $key) {
    $idnya                = $key['id_service'];
    $id_outbound          = $key['id_outbound'];
    $id_indes             = $key['id_indes'];
    $id_outbag            = $key['id_outbag'];

    $data_insert = array(
      'jenis'           => 'outbound-destinasi',
      'id_service'      => $idnya,
      'id_indes'        => $id_indes,
      'id_outbound'     => $id_outbound,
      'id_outbag'       => $id_outbag,
      'id_admin'        => $this->session->userdata('ses_id')
    );
    $this->db->insert('tabel_items', $data_insert);
  }
  echo json_encode($data);
  }
Harry Wardana
  • 277
  • 1
  • 5
  • 15

1 Answers1

1

You can use in_array to achieve this

public function nyobain(){

        $resi             = $this->input->post('kode');
        $expld            = explode(' ', $resi);
        $data             = $this->M_outbound_destinasi->dbGet($expld); 
    
         $db  = $this->db->query("SELECT id_service FROM `tabel_items`")->result();
    
   if(isset($data) && $data !== ''){//check if $data has a value and it not null

        if (in_array($data ,$db, true)) {

             return $data.'already exists in the DB!';

        }else{
    
            foreach ($data as $key) {
                 $idnya                = $key['id_service'];
                 $id_outbound          = $key['id_outbound'];
                 $id_indes             = $key['id_indes'];
                 $id_outbag            = $key['id_outbag'];
    
                    $data_insert = [
                         'jenis'           => 'outbound-destinasi',
                         'id_service'      => $idnya,
                         'id_indes'        => $id_indes,
                         'id_outbound'     => $id_outbound,
                         'id_outbag'       => $id_outbag,
                         'id_admin'        => $this->session->userdata('ses_id')
                      ];

                $this->db->insert('tabel_items', $data_insert);
            }
      
         return json_encode($data);

        }
   }
}
Hirumina
  • 738
  • 1
  • 9
  • 23