0

i have a problem for looping data and save to database. the result of this insert data contains only one. and what is the differences in insert() with insert_batch() ? i'm sorry CTRL + K on my keyboard is not function.

my view :

<?php echo form_open('proses_tambah_produk')?>
<input type="file" id="gambar2" name="gambar_tambah[]" class="form-control" style="width:90%;display:initial;margin-right:10px;margin-bottom:5px;">
<label style="background-color:red;color:white;border-radius:50%;padding:3px;" id="idGambar2" class="hapus_gambar glyphicon glyphicon-remove"></label>
<input type="file" id="gambar2" name="gambar_tambah[]" class="form-control" style="width:90%;display:initial;margin-right:10px;margin-bottom:5px;">
<label style="background-color:red;color:white;border-radius:50%;padding:3px;" id="idGambar2" class="hapus_gambar glyphicon glyphicon-remove"></label>
<?php echo form_close()?>

my controller :

function proses_tambah_produk(){
        $config['upload_path']          = 'assets/img/produk';
        $config['allowed_types']        = 'gif|jpg|png|jpeg';
        $config['max_size']             = 1000;
        $config['overwrite']             = TRUE;
        //$config['max_width']            = 1024;
        //$config['max_height']           = 768;
        $this->load->library('upload', $config);

        $files = $_FILES;
        $count = count($_FILES['gambar_tambah']['name']);
        for($i=0; $i<$count; $i++)
                {
                $_FILES['gambar_tambah']['name']= $files['gambar_tambah']['name'][$i];
                $_FILES['gambar_tambah']['type']= $files['gambar_tambah']['type'][$i];
                $_FILES['gambar_tambah']['tmp_name']= $files['gambar_tambah']['tmp_name'][$i];
                $_FILES['gambar_tambah']['error']= $files['gambar_tambah']['error'][$i];
                $_FILES['gambar_tambah']['size']= $files['gambar_tambah']['size'][$i];
                $this->upload->do_upload('gambar_tambah');
                $upload_data = $this->upload->data();
                $name_array[] = $upload_data['file_name'];
                $fileName = $upload_data['file_name'];
                $images[] = $fileName;

                }
              $fileName = $images;

            $tambahan = $_FILES['gambar_tambah']['name'];

            $this->produk_adm->add($data, $gambar, $tambahan);

    }

my model :

function add($tambahan){
        $last_insert_id = $this->db->insert_id();

        $data_gambar = array(
            'id_produk' => $last_insert_id,
            'gambar'    => $tambahan,
        );
        $this->db->insert('produk_image', $data_gambar);
        return $this->db->insert_id();
    }
2
  • You can only use the live Code Snippet feature for client-side JavaScript/HTML/CSS, which should be obvious as per the corresponding labels on each panel. Commented Feb 24, 2017 at 18:01
  • when you are calling model you have used $this->produk_adm->add($data, $gambar, $tambahan); Commented Feb 25, 2017 at 9:42

3 Answers 3

2
$filesCount = count($_FILES['photo_gallery']['name']);    
for($i = 0; $i < $filesCount; $i++){
                    $_FILES['gambar_tambah']['name'] = $_FILES['photo_gallery']['name'][$i];
                    $_FILES['gambar_tambah']['type'] = $_FILES['photo_gallery']['type'][$i];
                    $_FILES['gambar_tambah']['tmp_name'] = $_FILES['photo_gallery']['tmp_name'][$i];
                    $_FILES['gambar_tambah']['error'] = $_FILES['photo_gallery']['error'][$i];
                    $_FILES['gambar_tambah']['size'] = $_FILES['photo_gallery']['size'][$i];
                    $file_name=$this->crud->upload_file('gambar_tambah',$upload_image_path);
                    $image_data[$i]['image'] = $file_name;
                    $this->crud->add('table_name',$image_data[$i]);
                }
Sign up to request clarification or add additional context in comments.

Comments

0

you can't use same id name twice. The id value must be unique for every field. Your code for upload is wrong you are assigning value of $_FILES['gambar_tambah']['name']= $files['gambar_tambah']['name'][$i]; You can't do this insted you just assign value to a variable like $image_name = $_FILES['gambar_tambah']['name']; and insert that into database every time when loop runs or make array of it and insert it into one filed by implode function.

Comments

0

when you are calling model you have used three parameters and in model you are using one.

use

 $this->load->model('produk/adm');
 $this->produk_adm->add($tambahan);

instead of

$this->produk_adm->add($data, $gambar, $tambahan);

and in model

 function add($tambahan){
    $last_insert_id = $this->db->insert_id();

    $data_gambar = array(
        'id_produk' => $last_insert_id, // if it is auto increment in db, remove this line
        'gambar'    => $tambahan,
    );
    $this->db->insert('produk_image', $data_gambar);
    return $this->db->insert_id();
}

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.